PHP setting header information and getting return header information method

不言
Release: 2023-03-23 09:32:01
Original
4660 people have browsed it

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[&#39;query&#39;]) ? $temp[&#39;query&#39;] : &#39;&#39;;
 $path = isset($temp[&#39;path&#39;]) ? $temp[&#39;path&#39;] : &#39;/&#39;;
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp[&#39;host&#39;]}",
 "Content-Type: text/xml; charset=utf-8",
 &#39;Accept: */*&#39;,
 "Referer: http://{$temp[&#39;host&#39;]}/",
 &#39;User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)&#39;,
 "X-Forwarded-For: {$myIp}",
 "Content-length: 380",
 "Connection: Close"
 );
 return $header;
}
$interface = &#39;http://localhost/test/header2.php&#39;;
$header = FormatHeader($interface,&#39;10.1.11.1&#39;);
$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);
?>
Copy after login

2. The requested party obtains the header information, header2.php

<?php
print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的
?>
Copy after login

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>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
)
Copy after login

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); //取得返回头信息
Copy after login

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] => */*
Copy after login

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[&#39;query&#39;]) ? $temp[&#39;query&#39;] : &#39;&#39;;
 $path = isset($temp[&#39;path&#39;]) ? $temp[&#39;path&#39;] : &#39;/&#39;;
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp[&#39;host&#39;]}",
 "Content-Type: text/xml; charset=utf-8",
 &#39;Accept: */*&#39;,
 "Referer: http://{$temp[&#39;host&#39;]}/",
 &#39;User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)&#39;,
 "X-Forwarded-For: {$myIp}",
 "Content-length: " . strlen($xml) ."\r\n\r\n" .$xml, //修改1
 "Connection: Close"
 );
 return $header;
}
$xml = &#39;<?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>&#39;;
$interface = &#39;http://localhost/test/header2.php&#39;;
$header = FormatHeader($interface,&#39;10.1.11.1&#39;,$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);
?>
Copy after login

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(&#39;php://input&#39;, &#39;r&#39;);
var_dump($raw_post_data);
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!