PHP設(shè)置頭信息及取得返回頭信息的方法
本文實(shí)例講述了PHP設(shè)置頭信息及取得返回頭信息的方法。分享給大家供大家參考,具體如下:
設(shè)置請(qǐng)求的頭信息,我們可以用header函數(shù),可以用fsockopen,可以用curl等,本文主要講的是用curl來設(shè)置頭信息,并取得返回后的頭信息。
一、請(qǐng)求方設(shè)置自己的頭信息,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); //設(shè)置頭信息的地方
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);
?>
二、被請(qǐng)求方,取得頭信息,header2.php
<?php print_r($_SERVER); //頭信息里面有內(nèi)容絕大部分是放在系統(tǒng)變量里面的 ?>
三、看一下header.php請(qǐng)求的結(jié)果
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> 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 )
上面那幾個(gè),我們可以明顯看到,是我設(shè)置的頭信息。
四、取得返回的頭信息
我們把CURLOPT_HEADER設(shè)置成1,在取得的結(jié)果當(dāng)中,顯示數(shù)組的前面會(huì)有這些信息
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) ."\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); //設(shè)置頭信息的地方
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打印出來。這個(gè)時(shí)候,我們?cè)趆eader2.php后面加上以下二行
$raw_post_data = file_get_contents('php://input', 'r');
var_dump($raw_post_data);
這樣就可以取到$xml的內(nèi)容,并且只會(huì)取$xml的內(nèi)容。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《php curl用法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存簡單實(shí)例分析
這篇文章主要介紹了php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存,結(jié)合簡單實(shí)例形式分析了PHP System V共享內(nèi)存原理、相關(guān)函數(shù)與基本使用技巧,需要的朋友可以參考下2019-11-11
PHP中通過fopen()函數(shù)訪問遠(yuǎn)程文件示例
這篇文章主要介紹了PHP中通過fopen()函數(shù)訪問遠(yuǎn)程文件示例,本文講解了fopen函數(shù)的作用、使用它需要的配置問題、超時(shí)問題等內(nèi)容,并給出了代碼實(shí)例,需要的朋友可以參考下2014-11-11
php rmdir使用遞歸函數(shù)刪除非空目錄實(shí)例詳解
我們大家都知道,php rmdir()函數(shù)用于刪除空目錄,但如果要?jiǎng)h除非空目錄,我們必須將非空目錄中的文件或子目錄刪除,本文章向大家介紹php如何使用遞歸函數(shù)刪除非空目錄,需要的朋友可以參考一下2016-10-10
php實(shí)現(xiàn)與python進(jìn)行socket通信的方法示例
這篇文章主要介紹了php實(shí)現(xiàn)與python進(jìn)行socket通信的方法,結(jié)合實(shí)例形式分析了php使用自定義類發(fā)送socket請(qǐng)求數(shù)據(jù)及Python接收socket數(shù)據(jù)并處理請(qǐng)求等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

