PHP sends custom data through the header

Source: Internet
Author: User
This article describes how to send custom data through the header. When sending a request, in addition to using the $_get/ $_post send data, you can also put the data in the header to transmit the past.

Send header:

We define three parameters, token, language, region, and send the past to the header.


    $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'
';p rint_r ($data[' Post ']); Echo '
';Echo' Header data: ';Echo'
';p rint_r ($data[' header ']); Echo '
';/** * Send data * @param String $url requested address * @param Array $header Custom Header data * @param Ar Ray $content Post Data * @return 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 whether 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.


    $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);/** * Getting 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(    [name] => fdipzone)Header data:Array(    [token] => JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU    [language] => zh    [region] => GZ)

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above introduces PHP through the header to send custom data, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.