導航:首頁 > 編程語言 > php獲取httpheader

php獲取httpheader

發布時間:2025-05-19 18:01:07

php怎麼響應客戶端發送http請求

http請求有get,post。
php發送http請求有三種方式[我所知道的有三種,有其他的告訴我]。
1. file_get_contents();詳情見:http://www.jb51.net/article/41833.htm
2. curl發送請求。
3. fsocket發送。
下面說使用curl發送。
首先環境需要配置好curl組件。
在windows中讓php支持curl比較簡單:
在php.ini中將extension=php_curl.dll前面的分號去掉,
有人說需要將php根目錄的libeay32.dll和ssleay32.dll需要拷貝到系統目錄下去。我實驗不拷貝也可以。
linux中,如果使用源碼安裝,需要在make 之前,./configure --with-curl=path,
其中,path是你的 libcurl庫的位置,比如你安裝libcurl庫之後,
path可能就是/usr/local/,libcurl可以是靜態庫,也可以是動態庫。
注意libcurl庫configure的時候,可以將一些不需要的功能去掉,
比如ssl , ldap等。在php configure的時候,會去檢查libcurl中某些功能是否被開啟,進而去相應地調整生成的php。

❷ php如何獲取中文字元長度,一個中文字元算一個

在PHP中專門的mb_substr和mb_strlen函數,可以對中文進行截取和計算長度,但是,由於這些函數並非PHP的核心函數,所以,它們常常有可能沒有開啟。要在php.ini中開啟即可。獲取長度實例:1 ,utf-8環境下使用

header('Content-type:text/html;charset=utf-8');
/**
*可以統計中文字元串長度的函數
*@param$str要計算長度的字元串
*@param$type計算長度類型,0(默認)表示一個中文算一個字元,1表示一個中文算兩個字元
*
*/
functionabslength($str)
{
if(empty($str)){
return0;
}
if(function_exists('mb_strlen')){
returnmb_strlen($str,'utf-8');
}
else{
preg_match_all("/./u",$str,$ar);
returncount($ar[0]);
}
}
$str='我們都是中國人啊,ye!';
$len=abslength($str);
var_mp($len);//return12
$len=abslength($str,'1');
echo'<br/>'.$len;//return22

/*
utf-8編碼下截取中文字元串,參數可以參照substr函數
@param$str要進行截取的字元串
@param$start要進行截取的開始位置,負數為反向截取
@param$end要進行截取的長度
*/
functionutf8_substr($str,$start=0){
if(empty($str)){
returnfalse;
}
if(function_exists('mb_substr')){
if(func_num_args()>=3){
$end=func_get_arg(2);
returnmb_substr($str,$start,$end,'utf-8');
}
else{
mb_internal_encoding("UTF-8");
returnmb_substr($str,$start);
}

}
else{
$null="";
preg_match_all("/./u",$str,$ar);
if(func_num_args()>=3){
$end=func_get_arg(2);
returnjoin($null,array_slice($ar[0],$start,$end));
}
else{
returnjoin($null,array_slice($ar[0],$start));
}
}
}
$str2='wo要截取zhongwen';
echo'<br/>';
echoutf8_substr($str2,0,-4);//returnwo要截取zhon

2,支持gb2312,gbk,utf-8,big5 中文截取方法

/*

* 中文截取,支持gb2312,gbk,utf-8,big5

*

* @param string $str 要截取的字串

* @param int $start 截取起始位置

* @param int $length 截取長度

* @param string $charset utf-8|gb2312|gbk|big5 編碼

* @param $suffix 是否加尾綴

*/

public function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)

{

if(function_exists("mb_substr"))

{

if(mb_strlen($str, $charset) <= $length) return $str;

$slice = mb_substr($str, $start, $length, $charset);

}

else

{

$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";

$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";

$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";

$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";

preg_match_all($re[$charset], $str, $match);

if(count($match[0]) <= $length) return $str;

$slice = join("",array_slice($match[0], $start, $length));

}

if($suffix) return $slice."…";

return $slice;

}

❸ php 偽造 http_referer

function getContentCURL($url,$post_data = '',$user_agent="Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.8.1.3)", $header = 0, $follow_loc = 1, $cookie_file="/tmp/cookie.txt",$CURLOPT_TIMEOUT=30)
{
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
@curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
@curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
@curl_setopt($ch, CURLOPT_HEADER, $header);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_loc);
@curl_setopt($ch, CURLOPT_TIMEOUT, $CURLOPT_TIMEOUT);
if (trim($post_data) != '') {
@curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$result = @curl_exec($ch);
@curl_close($ch);

return $result;
}
通過這種方式做代理, 你在仔細研究下吧
參考下
http://www.21andy.com/blog/20080507/1095.html

❹ php curl的幾種用法

總結一下項目中用到curl的幾種方式 1. php curl的默認調用方法,get方式訪問url $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //設置http頭 curl_setopt($ch, CURLOPT_ENCODING, "gzip" ); //設置為客戶端支持gzip壓縮 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30 ); //設置連接等待時間 curl_setopt($ch, CURLOPT_URL, $url ); curl_exec( $ch ); if ($error = curl_error($ch) ) {//出錯處理return -1;}fclose($fp); $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //獲取http返回值 if( $curl_code == 200 ) { //正常訪問url}//異常 2. 設置http header支持curl訪問lighttpd伺服器Java代碼$header[]= 'Expect:'; $header[]= 'Expect:'; 3. 設置curl,只獲取http header,不獲取body:Java代碼curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); 或者只獲取body:Java代碼curl_setopt($ch, CURLOPT_HEADER, 0); // make sure we get the body curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_HEADER, 0); // make sure we get the body curl_setopt($ch, CURLOPT_NOBODY, 0); 4. 訪問虛擬主機,需設置Host $header[]= 'Host: '.$host; 5. 使用post, put, delete等REStful方式訪問urlpost:curl_setopt($ch, CURLOPT_POST, 1 ); put, delete: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); //或者PUT,需要伺服器支持這些方法。6. 保存下載內容為文件

❺ php curl中CURLOPT_HTTPHEADER 這個參數的含義

php curl中CURLOPT_HTTPHEADER 這個參數的含義是:CURLOPT_HTTPHEADER 一個用來設置HTTP頭欄位的數組。Content-Type 表示後面的文檔屬於什麼MIME類型。charset表示瀏覽器可接受的字元集。

HTTP頭Servlet默認為text/plain,但通常需要顯式地指定為text/html。由於經常要設置Content-Type,因此HttpServletResponse提供了一個專用的方法setContentType。

HTTP請求頭的部分類型:

1、Accept:瀏覽器可接受的MIME類型。

2、Accept-Charset:瀏覽器可接受的字元集。

3、Accept-Encoding:瀏覽器能夠進行解碼的數據編碼方式,比如gzip。Servlet能夠向支持gzip的瀏覽器返回經gzip編碼的HTML頁面。許多情形下這可以減少5到10倍的下載時間。

4、Connection:表示是否需要持久連接。如果Servlet看到這里的值為「Keep-Alive」,或者看到請求使用的是HTTP 1.1(HTTP 1.1默認進行持久連接),它就可以利用持久連接的優點,當頁麵包含多個元素時(例如Applet,圖片),顯著地減少下載所需要的時間。

5、Content-Length:表示請求消息正文的長度。

6、Cookie:這是最重要的請求頭信息之一。

(5)php獲取httpheader擴展閱讀:

PHP中的CURL函數庫(部分):

1、curl_setopt_array — 為cURL傳輸會話批量設置選項

2、curl_setopt — 設置一個cURL傳輸選項

3、curl_close — 關閉一個cURL會話

4、curl__handle — 復制一個cURL句柄和它的所有選項

5、curl_errno — 返回最後一次的錯誤號

6、curl_error — 返回一個保護當前會話最近一次錯誤的字元串

7、curl_escape — 使用 URL 編碼給定的字元串

在實際的使用當中,使用得最多的函數是curl_setopt — 設置一個cURL傳輸選項說明:bool curl_setopt ( resource $ch , int $option , mixed $value )其中,ch 由 curl_init() 返回的 cURL 句柄。option 表示的是需要設置的CURLOPT_XXX選項。

option的可選參數:

1、CURLOPT_BUFFERSIZE 每次獲取的數據中讀入緩存的大小,但是不保證這個值每次都會被填滿。在cURL 7.10中被加入。

2、CURLOPT_CLOSEPOLICY 不是CURLCLOSEPOLICY_LEAST_RECENTLY_USED就是CURLCLOSEPOLICY_OLDEST,還存在另外三個CURLCLOSEPOLICY_,但是cURL暫時還不支持。

3、CURLOPT_CONNECTTIMEOUT 在發起連接前等待的時間,如果設置為0,則無限等待。

❻ 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"要下載本軟體請到<ahref=http://www.df365.org>東方小屋</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");//fixforIE
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怎麼獲取表單提交的數據啊

一、用file_get_contents以get方式獲取內容,需要輸入內容為:

1、<?php

2、$url='http://www.domain.com/?para=123';

3、$html = file_get_contents($url);

4、echo $html;

5、?>

二、用file_get_contents函數,以post方式獲取url,需要輸入內容為

1、<?php

2、$url = 'http://www.domain.com/test.php?id=123';

3、$data = array ('foo' => 'bar');

4、$data = http_build_query($data);

5、$opts = array (

6、'http' => array (

7、 'method' => 'POST',

8、 'header'=> "Content-type: application/x-www-form-urlencoded " .

9、 "Content-Length: " . strlen($data) . " ",

10、 'content' => $data

11、)

12、);

13、$ctx = stream_context_create($opts);

14、$html = @file_get_contents($url,'',$ctx);

15、?>

三、用fopen打開url,以get方式獲取內容,需要輸入內容為

1、<?php

2、$fp = fopen($url, 'r');

3、$header = stream_get_meta_data($fp);//獲取信息

4、while(!feof($fp)) {

5、$result .= fgets($fp, 1024);

6、}

7、echo "url header: {$header} <br>":

8、echo "url body: $result";

9、fclose($fp);

10、?>

四、用fopen打開url,以post方式獲取內容,需要輸入內容為

1、<?php

2、$data = array ('foo2' => 'bar2','foo3'=>'bar3');

3、$data = http_build_query($data);

4、$opts = array (

5、'http' => array (

6、'method' => 'POST',

7、'header'=> "Content-type: application/x-www-form-urlencoded Cookie:cook1=c3;cook2=c4 " .

8、"Content-Length: " . strlen($data) . " ",

9、'content' => $data

10、)

11、);

12、$context = stream_context_create($opts);

13、$html = fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb' ,false, $context);

14、$w=fread($html,1024);

15、echo $w;

16、?>

五、用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body,需要輸入內容為

1、?php

2、function get_url ($url,$cookie=false)

3、{

4、$url = parse_url($url);

5、$query = $url[path]."?".$url[query];

6、echo "Query:".$query;

7、$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);

8、if (!$fp) {

9、return false;

10、} else {

11、$request = "GET $query HTTP/1.1 ";

12、$request .= "Host: $url[host] ";

13、$request .= "Connection: Close ";

14、if($cookie) $request.="Cookie: $cookie ";

15、$request.=" ";

16、fwrite($fp,$request);

17、while(!@feof($fp)) {

18、$result .= @fgets($fp, 1024);

19、}

20、fclose($fp);

21、return $result;

22、}

23、}

24、//獲取url的html部分,去掉header

25、function GetUrlHTML($url,$cookie=false)

26、{

27、$rowdata = get_url($url,$cookie);

28、if($rowdata)

29、{

30、$body= stristr($rowdata," ");

31、$body=substr($body,4,strlen($body));

32、return $body;

33、}

34、 return false;

35、}

36、?>

閱讀全文

與php獲取httpheader相關的資料

熱點內容
梁箍筋未標注加密區 瀏覽:627
自家網路連不上上面顯示加密 瀏覽:386
編譯後無法運行圖片 瀏覽:592
linux系統修改文件命令 瀏覽:702
iphone如何安裝中國石化app 瀏覽:176
app怎麼寫簡歷 瀏覽:680
金蝶kis雲app怎麼樣 瀏覽:708
cad命令xr 瀏覽:295
f如何設置ftp伺服器 瀏覽:833
編程題兔子生兔子python 瀏覽:421
加密數字卡專利申請 瀏覽:783
我的世界命令方塊該怎麼拿 瀏覽:785
浙江容錯伺服器廠家雲空間 瀏覽:196
linuxpython3idle 瀏覽:741
程序員成就感從哪來 瀏覽:547
游資抄底源碼公式 瀏覽:804
用VF命令 瀏覽:950
解壓速度14m 瀏覽:332
php獲取httpheader 瀏覽:300
什麼軟體可以修改pdf文件 瀏覽:869