"PHP" sends custom data through the header

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.