本文目錄一覽:
- 1、php 獲取網頁頭部信息和網頁和網頁源代碼查看
- 2、php如何獲取http頭部信息
- 3、如何在php中獲取curl請求的請求頭信息及相應頭信息
- 4、php如何獲得http post的數據?
- 5、php 如何獲取 客戶端http header
- 6、php中怎樣得到客戶端的http請求header所有信息
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如何獲取http頭部信息
var_dump(apache_request_headers());
//伺服器是apache的話 (我沒試過別的伺服器能否用)
http_get_request_headers()//需安裝 pecl_http 擴展
如何在php中獲取curl請求的請求頭信息及相應頭信息
獲取請求頭信息,可以在curl_exec函數執行前,添加代碼curl_setopt($ch,CURLINFO_HEADER_OUT,true);在curl_exec函數執行後,通過 curl_getinfo($ch,CURLINFO_HEADER_OUT) 來獲取curl執行請求的請求數據。
獲取響應頭信息,可以在curl_exec函數執行前,添加代碼 curl_setopt($ch, CURLOPT_HEADER, true);curl_setopt($ch, CURLOPT_NOBODY,true); 之後 通過curl_exec函數來獲取響應頭信息。獲取設置 curl_setopt($ch, CURLOPT_NOBODY,false);然後對curl_exec獲取的值通過\r\n\r\n進行分割截取第一部分即為響應頭信息。
php如何獲得http post的數據?
PHP獲取POST數據的幾種方法:
方法1、最常見的方法是:$_POST[‘fieldname’];
說明:只能接收Content-Type:
application/x-www-form-urlencoded提交的數據。
方法2、file_get_contents(“php://input”);
說明:
允許讀取
POST
的原始數據。
和
$HTTP_RAW_POST_DATA
比起來,它給內存帶來的壓力較小,並且不需要任何特殊的
php.ini
設置。
php://input
不能用於
enctype=”multipart/form-data”。
方法3、$GLOBALS[‘HTTP_RAW_POST_DATA’];
說明:
總是產生
$HTTP_RAW_POST_DATA
變數包含有原始的
POST
數據。
此變數僅在碰到未識別
MIME
類型的數據時產生。
php 如何獲取 客戶端http header
?php
$dir=$HTTP_GET_VARS[“dir”]; //…….取得上個頁面傳遞來的路徑
$file=$HTTP_GET_VARS[“file”]; //…….取得傳遞來的文件名
$url=parse_url($HTTP_REFERER); /*……取得前一頁面的URL地址,並將其放入一個數組中*/
if($url[host]!=$HTTP_HOST){echo “要下載本軟體請到a href=;東方小屋/a”;exit;} /*檢查來源網站是不是自己的網站,如果不是,返回「要下載本……」*/
if(empty($dir))$dir=”/”; //……如果路徑名為空,則為指定根目錄
if(empty($file)){echo “未指定要下載的文件!”;exit;} /*如果文件名為空,返回「未指定……」*/
$rootdir=”文件存放的根目錄”;//……你的下載路徑根目錄
$realurl=$rootdir.$dir; //…….取得你的下載目錄
chdir($realurl); //……將當前目錄轉到下載目錄中
if(!file_exists($file)){echo “對不起,此鏈接已經失效,請在下載頁面上向我們報告,謝謝!”;exit;} //……測試文件是否存在
$filename=$file;
//發送文件頭信息
header(“Cache-control: private”); // fix for IE
header(“Content-Type: application/octet-stream”);
header(“Content-Length: “.filesize($filename));
header(“Content-Disposition: attachment; filename=$filename”);
$fp = fopen($filename, ‘r’); // 以讀取方式打開指定文件
fpassthru($fp); // ** CORRECT ** 以二進位方式讀取文件
fclose($fp); // 關閉文件
?
php中怎樣得到客戶端的http請求header所有信息
?php
ob_end_flush();
print_r(apache_request_headers());
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/295333.html