本文目錄一覽:
- 1、php 獲取網頁頭部信息和網頁和網頁源代碼查看
- 2、php中頭信息已被發送的錯誤怎麼辦
- 3、請問PHP 怎樣請求時編寫頭信息並且還能獲得返回的頭信息?
- 4、php中http請求頭有什麼內容,由什麼組成
- 5、php如何獲取http頭部信息
- 6、如何在php中獲取curl請求的請求頭信息及相應頭信息
php 獲取網頁頭部信息和網頁和網頁源代碼查看
?php
/**
* http下載類庫
*/
class Httplib{
// 目標網站無法打開時返回的錯誤代碼
var $_ERROR_CONNECT_FAILURE = 600;
// 自定義 UserAgent 字符串
var $_SEND_USER_AGENT = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; LazyCMS.net::DownLoader)’;
var $_url,$_method,$_timeout;
var $_scheme,$_host,$_port,$_path,$_query,$_referer;
var $_header;
var $_response;
/**
* 兼容PHP5模式
*
* @param 同下面的參數
*/
function __construct($url=null,$method=’GET’,$timeout=60){
@set_time_limit(0);
if (!empty($url)) {
$this-connect($url,$method,$timeout);
}
return $this;
}
/**
* 初始化對象
*
* @param string $url
* @param string $method
* @param int $timeout
* @return object
*/
function Httplib($url=null,$method=’GET’,$timeout=60){
return $this-__construct($url,$method,$timeout);
}
/**
* 改變連接url
*
* @param string $url
* @param string $method
* @param int $timeout
* @return object
*/
function connect($url=null,$method=’GET’,$timeout=60){
$this-_header = null;
$this-_response = null;
$this-_url = $url;
$this-_method = strtoupper(empty($method) ? ‘GET’ : $method);
$this-_timeout = empty($timeout) ? 30 : $timeout;
if (!empty($url)) {
$this-_parseURL($url);
}
return $this;
}
/**
* 發送請求
*
* @param array $params
* @return bool
*/
function send($params=array()) {
$header = null; $response = null; $QueryStr = null;
if (!empty($params)) { $this-_method = ‘POST’; }
if (function_exists(‘fsockopen’)) {
$fp = @fsockopen($this-_host,$this-_port,$errno,$errstr,$this-_timeout);
if (!$fp) { return false; }
$_port = ((int)$this-_port!==80) ? ‘:’.$this-_port : null;
$SendStr = “{$this-_method} {$this-_path}{$this-_query} HTTP/1.0\r\n”;
$SendStr.= “Host:{$this-_host}{$_port}\r\n”;
$SendStr.= “Accept: */*\r\n”;
$SendStr.= “Referer:{$this-_referer}\r\n”;
$SendStr.= “User-Agent: “.$this-_SEND_USER_AGENT.”\r\n”;
$SendStr.= “Pragma: no-cache\r\n”;
$SendStr.= “Cache-Control: no-cache\r\n”;
//如果是POST方法,分析參數
if ($this-_method==’POST’) {
//判斷參數是否是數組,循環出查詢字符串
if (is_array($params)) {
$QueryStr = http_build_query($params);
} else {
$QueryStr = $params;
}
$length = strlen($QueryStr);
$SendStr.= “Content-Type: application/x-www-form-urlencoded\r\n”;
$SendStr.= “Content-Length: {$length}\r\n”;
}
$SendStr.= “Connection: Close\r\n\r\n”;
if(strlen($QueryStr) 0){
$SendStr.= $QueryStr.”\r\n”;
}
fputs($fp,$SendStr);
// 讀取 header
do{ $header.= fread($fp,1); } while (!preg_match(“/\r\n\r\n$/”,$header));
// 遇到跳轉,執行跟蹤跳轉
if ($this-_redirect($header)) { return true; }
// 讀取內容
while(!feof($fp)) {
$response.= fread($fp,4096);
}
fclose($fp);
} elseif (function_exists(‘curl_exec’)) {
$ch = curl_init($this-_url);
curl_setopt_array($ch,array(
CURLOPT_TIMEOUT = $this-_timeout,
CURLOPT_HEADER = true,
CURLOPT_RETURNTRANSFER = true,
CURLOPT_USERAGENT = $this-_SEND_USER_AGENT,
CURLOPT_REFERER = $this-_referer,
));
if ($this-_method==’GET’) {
curl_setopt($ch,CURLOPT_HTTPGET,true);
} else {
if (is_array($params)) {
$QueryStr = http_build_query($params);
} else {
$QueryStr = $params;
}
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$QueryStr);
}
$fp = curl_exec($ch);
curl_close($ch);
if (!$fp) { return false; }
$i = 0; $length = strlen($fp);
// 讀取 header
do{ $header.= substr($fp,$i,1); $i++; } while (!preg_match(“/\r\n\r\n$/”,$header));
// 遇到跳轉,執行跟蹤跳轉
if ($this-_redirect($header)) { return true; }
// 讀取內容
do {
$response.= substr($fp,$i,4096);
$i = $i + 4096;
} while ($length=$i);
unset($fp,$length,$i);
}
$this-_header = $header;
$this-_response = $response;
return true;
}
/**
* 跟蹤跳轉
*
* @param string $header
* @return bool
*/
function _redirect($header){
if (in_array($this-status($header),array(301,302))) {
if(preg_match(“/Location\:(.+)\r\n/i”,$header,$regs)){
$this-connect(trim($regs[1]),$this-_method,$this-_timeout);
$this-send();
return true;
}
} else {
return false;
}
}
/**
* 取得請求的header
*
* @return string
*/
function header(){
return $this-_header;
}
/**
* 請求返回的html
*
* @return string
*/
function response(){
return $this-_response;
}
/**
* 返回狀態
*
* @param string $header
* @return int
*/
function status($header=null){
if (empty($header)) {
$header = $this-_header;
}
if(preg_match(“/(.+) (\d+) (.+)\r\n/i”,$header,$status)){
return $status[2];
} else {
return $this-_ERROR_CONNECT_FAILURE;
}
}
/**
* 解析url
*
* @param string $url
*/
function _parseURL($url){
$aUrl = parse_url($url);
$aUrl[‘query’] = isset($aUrl[‘query’]) ? $aUrl[‘query’] : null;
$scheme = isset($_SERVER[‘HTTPS’]) ? $_SERVER[‘HTTPS’] : null;
$this-_scheme = ($scheme==’off’ || empty($scheme)) ? ‘http’ : ‘https’;
$this-_host = isset($aUrl[‘host’]) ? $aUrl[‘host’] : null;
$this-_port = empty($aUrl[‘port’]) ? 80 : (int)$aUrl[‘host’];
$this-_path = empty($aUrl[‘path’]) ? ‘/’ : (string)$aUrl[‘path’];
$this-_query = strlen($aUrl[‘query’]) 0 ? ‘?’.$aUrl[‘query’] : null;
$this-_referer = $this-_scheme.’://’.$aUrl[‘host’];
}
}
$http = new Httplib(”);
$http-send();
$body = $http-response();
echo $body;
php中頭信息已被發送的錯誤怎麼辦
首先,你的文件是不是UTF-8編碼,如果是那就需要專用工具去掉文件前面的三個位元組(俗稱BOM頭),EDITPLUS等軟件就能另外為無BOM的UTF-8編碼文件。
如果不是UTF-8編碼,或者已經去除了BOM,那麼要保證header語句之前沒有輸出內容,文件的第一個?php前面沒有任何空白、回車。或者在文件的最前面添加下面的內容:
?PHP
ob_start();
?
請問PHP 怎樣請求時編寫頭信息並且還能獲得返回的頭信息?
一種使用fsockopen,所有的頭信息全部手動連接成字符串即可。
一種使用curl,可以直接將頭信息寫成數組,調用curl_setopt設定頭信息即可。
php中http請求頭有什麼內容,由什麼組成
PHP中一般採用getallheaders來獲取頭部,但事實上,有些模式下是獲取不到的(以前真沒有注意過在fastcgi下這個函數不能用)
在PHP里,想要得到所有的HTTP請求頭,可以使用getallheaders方法,不過此方法並不是在任何環境下都存在,比如說,你使用fastcgi方式運行PHP的話,就沒有這個方法,所以說我們還需要考慮別的方法,幸運的是$_SERVER里有我們想要的東西,它裏面鍵名以HTTP_開頭的就是HTTP請求頭:
$headers = array();
foreach ($_SERVER as $key = $value) {
if (‘HTTP_’ == substr($key, 0, 5)) {
$headers[str_replace(‘_’, ‘-‘, substr($key, 5))] = $value;
}
}
代碼很簡單,需要說明的是RFC里明確指出了信息頭的名字是不區分大小寫的。
不過並不是所有的HTTP請求頭都是以HTTP_開頭的的鍵的形式存在與$_SERVER里,比如說Authorization,Content-Length,Content-Type就不是這樣,所以說為了取得所有的HTTP請求頭,還需要加上下面這段代碼:
if (isset($_SERVER[‘PHP_AUTH_DIGEST’])) {
$header[‘AUTHORIZATION’] = $_SERVER[‘PHP_AUTH_DIGEST’]);
} elseif (isset($_SERVER[‘PHP_AUTH_USER’]) isset($_SERVER[‘PHP_AUTH_PW’])) {
$header[‘AUTHORIZATION’] = base64_encode($_SERVER[‘PHP_AUTH_USER’] . ‘:’ . $_SERVER[‘PHP_AUTH_PW’]));
}
if (isset($_SERVER[‘CONTENT_LENGTH’])) {
$header[‘CONTENT-LENGTH’] = $_SERVER[‘CONTENT_LENGTH’];
}
if (isset($_SERVER[‘CONTENT_TYPE’])) {
$header[‘CONTENT-TYPE’] = $_SERVER[‘CONTENT_TYPE’];
}
php如何獲取http頭部信息
var_dump(apache_request_headers());
//服務器是apache的話 (我沒試過別的服務器能否用)
http_get_request_headers()//需安裝 pecl_http 擴展
如何在php中獲取curl請求的請求頭信息及相應頭信息
oCurl = curl_init();
// 設置請求頭
$header[] = “Content-type: application/x-www-form-urlencoded”;
$user_agent = “Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36”;
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header);
// 返回 response_header, 該選項非常重要,如果不為 true, 只會獲得響應的正文
curl_setopt($oCurl, CURLOPT_HEADER, true);
// 是否不需要響應的正文,為了節省帶寬及時間,在只需要響應頭的情況下可以不要正文
curl_setopt($oCurl, CURLOPT_NOBODY, true);
// 使用上面定義的 ua
curl_setopt($oCurl, CURLOPT_USERAGENT,$user_agent);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
// 不用 POST 方式請求, 意思就是通過 GET 請求
curl_setopt($oCurl, CURLOPT_POST, false);
$sContent = curl_exec($oCurl);
// 獲得響應結果里的:頭大小
$headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
// 根據頭大小去獲取頭信息內容
$header = substr($sContent, 0, $headerSize);
curl_close($oCurl);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/254641.html