PHP Set header information and get the return header information method

Source: Internet
Author: User
Tags sha1
This article mainly introduces the PHP configuration header information and the method to obtain the return header information, combined with an example of PHP based on the head information of the operation of the tips, friends can refer to the following

This paper describes the PHP setup header information and the method of obtaining the return header information. Share to everyone for your reference, as follows:

Set the request header information, we can use the header function, you can use Fsockopen, you can use curl, etc., this article is mainly about using curl to set the header information, and get back the header information.

First, the requester set its own header information, header.php

<?phpfunction formatheader ($url, $myIp = null, $xml = NULL) {//extract URL $temp = Pars E_url ($url); $query = Isset ($temp [' query '])? $temp [' query ']: '; $path = Isset ($temp [' path '])? $temp [' Path ']: '/'; $header = Array ("POST {$path}?{ $query} http/1.1 "," host: {$temp [' Host ']} "," Content-type:text/xml; Charset=utf-8 ", ' Accept: */* '," referer:http://{$temp [' Host ']}/', ' user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1) ', ' x-forwarded-for: {$myIp} ', ' content-length:380 ', ' connection:close '); return $header;} $interface = ' http://localhost/test/header2.php '; $header = Formatheader ($interface, ' 10.1.11.1 '); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $interface); curl_setopt ($ch, Curlopt_httpheader, $header); Set header information where curl_setopt ($ch, Curlopt_header, 0); Do not get return header information curl_setopt ($ch, Curlopt_timeout, 5); curl_setopt ($ch, Curlopt_returntransfer, true); $result = Curl_exec ($ CH); Var_dump ($result);? 

Second, the requested party, obtain the head information, header2.php

<?phpprint_r ($_server); Most of the information inside the header is placed in the system variable?>

Third, look at the results of the header.php request

String (1045) "Array ([http_host] = localhost[content_type] = text/xml; Charset=utf-8[http_accept] = */*[http_referer] = http://localhost/[http_user_agent] = mozilla/4.0 ( Compatible MSIE 7.0; Windows NT 5.1; SV1) [http_x_forwarded_for] = 10.1.11.1[content_length] = 380[path] =/usr/local/bin:/usr/bin:/bin[server _signature] = <address>apache/2.2.16 (Ubuntu) Server at localhost Port 80</address> ............ 。。。。。。 )

The above ones, we can clearly see, is the header information I set.

Iv. obtaining the return header information

Copy the Code code as follows:

curl_setopt ($ch, Curlopt_header, 1); Get Return header information

We set the Curlopt_header to 1, and in the results we get, we have this information in front of the array.

String (1239) "http/1.1 Okdate:fri, 01:57:57 gmtserver:apache/2.2.16 (Ubuntu) X-powered-by:php/5.3.3-1ubu Ntu9.5vary:accept-encodingcontent-length:1045content-type:text/htmlarray ([http_host] = localhost [CONTENT_ TYPE] = Text/xml; Charset=utf-8 [http_accept] = */*

Five, $_server part of the head information is not get

Modify the header.php

<?phpfunction Formatheader ($url, $myIp = null, $xml = NULL) {//extract URL $temp = Parse_url ($url); $query = Isset ($temp [' Que Ry '])? $temp [' query ']: '; $path = Isset ($temp [' path '])? $temp [' Path ']: '/'; $header = Array ("POST {$path}?{ $query} http/1.1 "," host: {$temp [' Host ']} "," Content-type:text/xml; Charset=utf-8 ", ' Accept: */* '," referer:http://{$temp [' Host ']}/', ' user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1) ', ' x-forwarded-for: {$myIp} ', ' Content-length: '. Strlen ($xml). " \r\n\r\n ". $xml,//Modify 1" connection:close "); return $header;} $xml = ' <?xml version= ' 1.0 "encoding=" Utf-8 "?>//Modify 2 <profile> <sha1>adsfadsf</sha1> <user_ Id>asdfasdf</user_id> <album_id>asdf</album_id> <album_name>asdf</album_name> <tags>asdfasd</tags> <title>asdfasdf</title> <content>asdfadsf</content> < Type>asdfasdf</type> <copyright>asdfasdf</copyright> </profile>'; $interface = ' http://localhost/test/header2.php '; $header = Formatheader ($interface, ' 10.1.11.1 ', $xml); Modified 3$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $interface); curl_setopt ($ch, Curlopt_httpheader, $header); Set header information where curl_setopt ($ch, Curlopt_header, 0); Do not get return header information curl_setopt ($ch, Curlopt_timeout, 5); curl_setopt ($ch, Curlopt_returntransfer, true); $result = Curl_exec ($ CH); Var_dump ($result);? >

If so, header2.php inside, print $_server cannot print out the XML in the head information. At this point, we'll add the following two lines to the back of header2.php.

$raw _post_data = file_get_contents (' php://input ', ' R '); Var_dump ($raw _post_data);

This allows the content to be $xml and only $xml content is taken.

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.