How to get PHP to send custom data through the header

Source: Internet
Author: User
Tags php json
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_dec Ode ($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 the information * @param String $url The requested address * @param     The 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 certificate Check curl_setopt ($ch, C  Urlopt_ssl_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)

This article explains how to get PHP to send custom data through the header, and more about the PHP Chinese web.

Related recommendations:

How to use Inet_aton and Inet_ntoa to process IP address data via MySQL

PHP JSON data uses gzip to compress output related content

How to use HTTP_BUILD_QUERY,PARSE_URL,PARSE_STR to create and resolve URLs using PHP

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.