Home > Backend Development > PHP Tutorial > php sends custom data through header

php sends custom data through header

WBOY
Release: 2016-07-29 09:12:41
Original
998 people have browsed it

This article will introduce how to send custom data through headers. When sending a request, in addition to using $_GET/$_POST to send data, you can also put the data in the header and transmit it.

Send header:

We have defined three parameters, token, language, region, put the header and send it over

<code><span><span><?php</span><span>$url</span> = <span>'http://www.example.com'</span>;
<span>$header</span> = <span>array</span>(<span>'token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU'</span>,<span>'language:zh'</span>,<span>'region:GZ'</span>);
<span>$content</span> = <span>array</span>(
        <span>'name'</span> => <span>'fdipzone'</span>
);

<span>$response</span> = tocurl(<span>$url</span>, <span>$header</span>, <span>$content</span>);
<span>$data</span> = json_decode(<span>$response</span>, <span>true</span>);

<span>echo</span><span>'POST data:'</span>;
<span>echo</span><span>'<pre class="brush:php;toolbar:false">'</span>;
print_r(<span>$data</span>[<span>'post'</span>]);
<span>echo</span><span>'
'; echo'Header data:'; echo'
'</span>;
print_r(<span>$data</span>[<span>'header'</span>]);
<span>echo</span><span>'
'
; /** * 发送数据 * @param String $url 请求的地址 * @param Array $header 自定义的header数据 * @param Array $content POST的数据 * @return String */functiontocurl($url, $header, $content){$ch = curl_init(); if(substr($url,0,5)=='https'){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在 } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); return$response; } ?>
Copy after login

Receive header

We can get the header data in $_SERVER, Custom data is prefixed with HTTP_, so the data with HTTP_ prefix can be read out.

<code><span><span><?php</span><span>$post_data</span> = <span>$_POST</span>;
<span>$header</span> = get_all_headers();

<span>$ret</span> = <span>array</span>();
<span>$ret</span>[<span>'post'</span>] = <span>$post_data</span>;
<span>$ret</span>[<span>'header'</span>] = <span>$header</span>;

header(<span>'content-type:application/json;charset=utf8'</span>);
<span>echo</span> json_encode(<span>$ret</span>, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

<span>/**
 * 获取自定义的header数据
 */</span><span><span>function</span><span>get_all_headers</span><span>()</span>{</span><span>// 忽略获取的header数据</span><span>$ignore</span> = <span>array</span>(<span>'host'</span>,<span>'accept'</span>,<span>'content-length'</span>,<span>'content-type'</span>);

    <span>$headers</span> = <span>array</span>();

    <span>foreach</span>(<span>$_SERVER</span><span>as</span><span>$key</span>=><span>$value</span>){
        <span>if</span>(substr(<span>$key</span>, <span>0</span>, <span>5</span>)===<span>'HTTP_'</span>){
            <span>$key</span> = substr(<span>$key</span>, <span>5</span>);
            <span>$key</span> = str_replace(<span>'_'</span>, <span>' '</span>, <span>$key</span>);
            <span>$key</span> = str_replace(<span>' '</span>, <span>'-'</span>, <span>$key</span>);
            <span>$key</span> = strtolower(<span>$key</span>);

            <span>if</span>(!in_array(<span>$key</span>, <span>$ignore</span>)){
                <span>$headers</span>[<span>$key</span>] = <span>$value</span>;
            }
        }
    }

    <span>return</span><span>$headers</span>;

}
<span>?></span></span></span></code>
Copy after login

Output:

<code>POST data:
<span>Array</span>
(
    [name] => fdipzone
)
Header data:
<span>Array</span>
(
    [token] => JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU
    [language] => zh
    [region] => GZ
)</code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how PHP sends custom data through headers, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template