麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

PHP設置頭信息及取得返回頭信息的方法

2024-05-04 23:42:31
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP設置頭信息及取得返回頭信息的方法,結合實例分析了PHP基于curl針對頭信息的操作技巧,需要的朋友可以參考下
 

本文實例講述了PHP設置頭信息及取得返回頭信息的方法。分享給大家供大家參考,具體如下:

設置請求的頭信息,我們可以用header函數,可以用fsockopen,可以用curl等,本文主要講的是用curl來設置頭信息,并取得返回后的頭信息。

一、請求方設置自己的頭信息,header.php

<?phpfunction 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

<?phpprint_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 OKDate: Fri, 27 May 2011 01:57:57 GMTServer: Apache/2.2.16 (Ubuntu)X-Powered-By: PHP/5.3.3-1ubuntu9.5Vary: Accept-EncodingContent-Length: 1045Content-Type: text/htmlArray( [HTTP_HOST] => localhost [CONTENT_TYPE] => text/xml; charset=utf-8 [HTTP_ACCEPT] => */*

五、$_SERVER部分頭信息是拿不到的

修改一下header.php

<?phpfunction 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);?>

如果這樣的話,header2.php里面,打印$_SERVER不可能把頭信息中的xml打印出來。這個時候,我們在header2.php后面加上以下二行

$raw_post_data = file_get_contents('php://input', 'r');var_dump($raw_post_data);

這樣就可以取到$xml的內容,并且只會取$xml的內容。

 

希望本文所述對大家PHP程序設計有所幫助。



注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 激情小说区 | 新久草在线视频 | 中文字幕网在线 | 九九热视频在线免费观看 | 精品成人国产在线观看男人呻吟 | 日本人乱人乱亲乱色视频观看 | 极品销魂一区二区三区 | 日本欧美国产 | 国产亚洲精品久久久久婷婷瑜伽 | 成人毛片免费视频 | 午夜a狂野欧美一区二区 | 欧美一级小视频 | 黄色网址在线播放 | 国产免费资源 | 国产88久久久国产精品免费二区 | 宅男噜噜噜66国产在线观看 | 国产在线91| 久色一区 | 亚洲精品欧美二区三区中文字幕 | 欧美一区成人 | av成人在线免费观看 | 电影av在线 | 久久综合久久美利坚合众国 | 性少妇videosexfreexx | 国产乱淫a∨片免费观看 | 精品国产一区二区三区久久久 | 亚洲国产美女视频 | 91九色蝌蚪国产 | 成人18免费观看 | 欧美精品一区二区久久 | 久久久久久亚洲国产精品 | 久久精品一区二区三区不卡牛牛 | 羞羞视频免费观看网站 | 欧美三级短视频 | 91羞羞| 欧美日韩国产一区二区三区在线观看 | 一区二区三区视频在线观看 | 在线小视频国产 | 国产一级在线观看视频 | 4399一级成人毛片 | 精品国产91久久久久久久妲己 |