PHP Set header information, get Return header information
Zhang Ying published in 2011-05-27
Category: php
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.
One, the requester sets its own header information, header.php
View copy print?
- <?php
- function Formatheader ($url, $myIp = null,$xml = null)
- {
- //Extract URL
- $temp = parse_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); //Where to set the header information
- 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
View copy print?
- <?php
- Print_r ($_server); //header information in most of the content is placed inside the system variables
- ?>
Three, 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.
Four, get back the header information
View copy print?
- 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.
View copy print?
- String (1239) "http/1.1 OK
- Date:fri, 01:57:57 GMT
- server:apache/2.2.16 (Ubuntu)
- x-powered-by:php/5.3.3-1ubuntu9.5
- Vary:accept-encoding
- content-length:1045
- Content-type:text/html
- Array
- (
- [Http_host] = localhost
- [Content_Type] = Text/xml; Charset=utf-8
- [Http_accept] = */*
- 。。。。。。。。。。。。。。。。
Five, $_server part of the header information is not to be taken
Modify the header.php
View copy print?
- <?php
- function Formatheader ($url, $myIp = null,$xml = null)
- {
- //Extract URL
- $temp = parse_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:". 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); //Modify 3
- $ch = Curl_init ();
- curl_setopt ($ch, Curlopt_url, $interface);
- curl_setopt ($ch, Curlopt_httpheader, $header); //Where to set the header information
- 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.
View copy print?
- $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.
PHP Set header information, get Return header information