Home >Backend Development >PHP Tutorial >PHP setting header information and getting return header information method
This article mainly introduces the method of setting header information and obtaining return header information in PHP, and analyzes PHP's curl-based operation skills for header information with examples. Friends in need can refer to the following
Example descriptions 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 Get the returned header information.
1. The requesting party 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 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> 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 )
We can clearly see that the above ones are the header information I set.
4. Obtain the returned header information
Copy code The code is as follows:
curl_setopt($ch, CURLOPT_HEADER, 1); //取得返回头信息
We set CURLOPT_HEADER Into 1, among 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 header information of the $_SERVER part cannot be obtained
Modify it 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
$raw_post_data = file_get_contents('php://input', 'r'); var_dump($raw_post_data);
after header2.php so that the content of $xml can be obtained, and only the content of $xml will be obtained.
Related recommendations:
Detailed explanation of how PHP sets the HTTPONLY attribute of Cookie
Set server (Apache/Nginx) environment variables for PHP
The above is the detailed content of PHP setting header information and getting return header information method. For more information, please follow other related articles on the PHP Chinese website!