Send header:
We define three parameters,token, language,region, and send the past to the header.
<?PHP$url= ' http://www.example.com ';$header=Array(' Token:jxrazezavm3hxm3d9pwnyiqqqc1sjbsu ', ' Language:zh ', ' region:gz ');$content=Array( ' Name ' = ' Fdipzone ');$response= Tocurl ($url,$header,$content);$data= Json_decode ($response,true);Echo' POST data: ';Echo' <pre> ';Print_r($data[' Post ']);Echo' </pre> ';Echo' Header data: ';Echo' <pre> ';Print_r($data[' Header ']);Echo' </pre> ';/** * Send data * @param String $url requested address * @param array $header custom Header data * @param array $content post data * @retu RN String*/functionTocurl ($url,$header,$content){ $ch=Curl_init (); if(substr($url, 0,5) = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer,false);//Skip Certificate Checkcurl_setopt ($ch, Curlopt_ssl_verifyhost,true);//Check that the SSL encryption algorithm exists from the certificate} 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;}?>
Receive Header
We can get header data in $_server , and the custom data is prefixed with http_ , so we can read the data of the HTTP_ prefix.
<?PHP$post _data=$_post;$header=get_all_headers ();$ret=Array();$ret[' post '] =$post _data;$ret[' header '] =$header;Header(' Content-type:application/json;charset=utf8 ');EchoJson_encode ($ret, json_unescaped_unicode|json_pretty_print);/** * Get custom Header Data*/functionget_all_headers () {//Ignore fetched header data $ignore=Array(' Host ', ' Accept ', ' content-length ', ' Content-type '); $headers=Array(); foreach($_server as $key=$value){ if(substr($key, 0, 5) = = = ' Http_ '){ $key=substr($key, 5); $key=Str_replace(‘_‘, ‘ ‘,$key); $key=Str_replace(‘ ‘, ‘-‘,$key); $key=Strtolower($key); if(!In_array($key,$ignore)){ $headers[$key] =$value; } } } return $headers;}?>
Output:
POST data:array( = = fdipzone)Header data:array( = = JXRAZEZAVM3HXM3D9PWNYIQQQC1SJBSU= en= GZ)
"PHP" sends custom data through the header