The example in this article describes how to set header information and obtain return header information in PHP. Share it with everyone for your reference, the details are as follows:
To set the header information of the request, we can use the header function, fsockopen, curl, etc. This article mainly talks about using curl to set the header information and obtain the returned header information.
1. The requester sets its own header information, header.php
<?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); ?>
2. The requested party obtains the header information, header2.php
<?php print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的 ?>
3. Take a look at the result of 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> 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 )
We can clearly see that the above ones are the header information I set.
4. Obtain the returned header information
We set CURLOPT_HEADER to 1, and in the obtained results, there will be this information in front of the display array
string(1239) "HTTP/1.1 200 OK Date: Fri, 27 May 2011 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] => */*
5. The $_SERVER header information cannot be obtained
Modify header.php
<?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 this is the case, in header2.php, printing $_SERVER cannot print out the xml in the header information. At this time, we add the following two lines
after header2.php$raw_post_data = file_get_contents('php://input', 'r'); var_dump($raw_post_data);
In this way, you can get the content of $xml, and only the content of $xml will be fetched.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction Tutorial on PHP Basic Syntax", "Introduction Tutorial on PHP Object-Oriented Programming" and " php curl usage summary》
I hope this article will be helpful to everyone in PHP programming.