本文將介紹如何透過header傳送自訂資料。發送請求時,除了可以使用$_GET/$_POST發送數據,也可以把數據放在header中傳輸過去。
發送header:
我們定義了三個參數,token、language、region,放入header發送過去
<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; } ?>
自訂的資料都是使用HTTP_作為前綴的,所以可以把HTTP_前綴的資料讀出。
<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>
輸出:
<code>POST data: <span>Array</span> ( [name] => fdipzone ) Header data: <span>Array</span> ( [token] => JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU [language] => zh [region] => GZ )</code>
以上就介紹了php透過header發送自訂數據,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。