Php uses the header to send custom data.

Source: Internet
Author: User

Php uses the header to send custom data.

This article describes how to send custom data through the header. When sending a request, you can use $ _ GET/$ _ POST to send data, or put the data in the header for transmission.

Sending header:

We have defined three parameters,Token,Language,Region, PutHeaderSent in

<? 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 ['head']); echo' </pre> '; /*** send data ** @ param S Tring $ url request address * @ param Array $ header custom header data * @ param Array $ content POST Data * @ return String */function tocurl ($ url, $ header, $ content) {$ ch = curl_init (); if (substr ($ url, 0,5) = 'https') {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false ); // skip the certificate check curl_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_U RL, $ 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 the header data in $ _ SERVER. Custom Data uses HTTP _ as the prefix, so we can read the HTTP _ prefix data.

<? Php $ post_data =$ _ POST; $ header = get_all_headers (); $ ret = array (); $ ret ['post'] = $ post_data; $ ret ['header'] = $ header; header ('content-type: application/json; charset = utf8'); echo json_encode ($ ret, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ); /*** get custom header data */function get_all_headers () {// ignore the obtained header data $ ignore = array ('host', 'access ', 'content-length', 'content-type'); $ headers = array (); foreach ($ _ SE RVER 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)

The above php Method for sending custom data through the header is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.