2345678910111213141516171819202122232425262728293031323334353637383940414243<?phpfunction FormatHeader ($url, $myIp = null, $xml = NULL) {//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 (compatib Le 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://wang.com/curl/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, 1); 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);? >
<?php/** * Created by Phpstorm. * User:brady * DATE:2017/12/11 * time:19:50 *///print_r ($_server); The contents of the header are mostly in the system variables $raw_post_data = file_get_contents (' php://input ', ' R '); Var_dump ($raw _post_data);
Reference
Http://www.jb51.net/article/78609.htm
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
?
12345678910111213141516171819202122232425262728293031 |
<?php
function FormatHeader(
$url
,
$myIp = null,
$xml = null)
{
// 解悉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
);
//设置头信息的地方
curl_setopt(
$ch
, CURLOPT_HEADER, 0);
//不取得返回头信息
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
?
123 |
<?php print_r( $_SERVER ); //头信息里面有内容绝大部分是放在系统变量里面的 ?> |
Third, look at the results of the header.php request
?
12345678910111213 |
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 CodeThe code is 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.
?
123456789101112 |
string (1239) "http/1.1 OK date server:apache/2.2.16 (Ubuntu) Code class= "PHP plain" >x-powered-by:php/5.3.3-1ubuntu9.5 vary:accept-encoding content-length:1045 content-type:text/html array ( &NBSP; [http_host] = localhost &NBSP; [content_type] = text/xml; Charset=utf-8 &NBSP; [http_accept] = */* |
Five, $_server part of the head information is not get
Modify the header.php
?
12345678910111213141516171819202122232425262728293031323334353637383940414243 |
<?php
function FormatHeader(
$url
,
$myIp = null,
$xml = null)
{
// 解悉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
,
//修改1
"Connection: Close"
);
return $header
;
}
$xml = ‘<?xml version=
"1.0" encoding=
"utf-8"
?>
//修改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
);
//修改3
$ch = curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$interface
);
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
$header
);
//设置头信息的地方
curl_setopt(
$ch
, CURLOPT_HEADER, 0);
//不取得返回头信息
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.
?
12 |
$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.
Curl Set Head