本文实例讲述了php设置头信息及取得返回头信息的方法。分享给大家供大家参考,具体如下:
设置请求的头信息,我们可以用header函数,可以用fsockopen,可以用curl等,本文主要讲的是用curl来设置头信息,并取得返回后的头信息。
一、请求方设置自己的头信息,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);
?>
二、被请求方,取得头信息,header2.php
<?php
print_r($_server); //头信息里面有内容绝大部分是放在系统变量里面的
?>
三、看一下header.php请求的结果
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>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
)
上面那几个,我们可以明显看到,是我设置的头信息。
四、取得返回的头信息
复制代码 代码如下:
curl_setopt($ch, curlopt_header, 1); //取得返回头信息
我们把curlopt_header设置成1,在取得的结果当中,显示数组的前面会有这些信息
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] => */*
五、$_server部分头信息是拿不到的
修改一下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) ."rnrn" .$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);
?>
如果这样的话,header2.php里面,打印$_server不可能把头信息中的xml打印出来。这个时候,我们在header2.php后面加上以下二行
$raw_post_data = file_get_contents('php://input', 'r');
var_dump($raw_post_data);
这样就可以取到$xml的内容,并且只会取$xml的内容。
更多关于php相关内容感兴趣的读者可查看本站专题:《php基本语法入门教程》、《php面向对象程序设计入门教程》及《php curl用法总结》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!