PHP sends custom data methods via header

Source: Internet
Author: User
The following small series for you to share a PHP through the header to send a custom data method, with a good reference and learn the value of PHP, we hope to help. Interested in PHP to follow the small part of it come to see it

This article describes how to send custom data through the header. When sending a request, you can also transfer the data in the header, in addition to using $_get/$_post to send the data.

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> ';p rint_r ($data [' POST ']); Echo ' </pre> '; Echo ' Header data: '; Echo ' <pre> ';p rint_r ($data [' Header ']), echo ' </pre> ';/** * Send data * @param String $url The requested address * @param Array $he Ader 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 certificate Check curl_setopt ($ch, curlopt_s Sl_verifyhost, True);  Check the existence of the SSL encryption algorithm 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 '); Echo Json_encode ($ret, json_unescaped_unicode| Json_pretty_print)/** * Gets the custom header data */function get_all_headers () {  //ignores the header data obtained  $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] = en  [Region] = GZ)

Above this PHP through the header to send custom data method is small part to share all the content of everyone, hope to give you a reference!!

Related recommendations:

PHP implementation can add watermark and generate thumbnail processing tool

PHP implementation to find out the list of links in the link list of the entry node detailed

A detailed explanation of the PHP serialization and deserialization principles

Related Article

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.