首頁 >php教程 >php手册 >PHP發送請求頭和​​接收列印請求頭

PHP發送請求頭和​​接收列印請求頭

WBOY
WBOY原創
2016-09-14 09:24:011451瀏覽

一、發送請求頭

<span style="color: #008000;">//</span><span style="color: #008000;">发送地址</span>
<span style="color: #800080;">$url</span> = 'http://127.0.0.1/2.php'<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">请求头内容</span>
<span style="color: #800080;">$headers</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
    </span>'Authorization: '.<span style="color: #800080;">$basic</span>,
    'suibianzhi: '.<span style="color: #800080;">$basic</span>,<span style="color: #000000;">
);
</span><span style="color: #008000;">//</span><span style="color: #008000;">使用curl发送</span>
<span style="color: #800080;">$ch</span> = curl_init(<span style="color: #800080;">$url</span><span style="color: #000000;">);
curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #0000ff;">true</span><span style="color: #000000;">);
curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_HTTPHEADER, <span style="color: #800080;">$headers</span><span style="color: #000000;">);
</span><span style="color: #800080;">$result</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result</span>;

二、接收並列印請求頭

<span style="color: #800080;">$headers</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$_SERVER</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {
    </span><span style="color: #0000ff;">if</span> ('HTTP_' == <span style="color: #008080;">substr</span>(<span style="color: #800080;">$key</span>, 0, 5<span style="color: #000000;">)) {
        </span><span style="color: #800080;">$headers</span>[<span style="color: #008080;">str_replace</span>('_', '-', <span style="color: #008080;">substr</span>(<span style="color: #800080;">$key</span>, 5))] = <span style="color: #800080;">$value</span><span style="color: #000000;">;
    }
}    
</span><span style="color: #0000ff;">echo</span> '<pre class="brush:php;toolbar:false">'<span style="color: #000000;">;
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$headers</span>);  

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn