如果支援開啟遠端內容的選項的話,實際上php用fopen或file_get_contents都能獲得一個網頁的內容,但是預設的函數有個不足的地方就是無法取得HTTP頭,這在一些特殊的應用程式中很不方便,如,有一個連結:
http://www.abc.com/showvd.asp?id=18
假如它返回的是一個圖片,用預設函數就很難識別,但如果透過HTTP應答頭來判斷就簡單多了,此外如果對方透過 Refer 來防盜鏈的話,也是無法取得的,用HTTP類就能完美解決這些問題,而且速度也相差無幾。
使用方法:
$hd = new DedeHttpDown();
$hd->OpenUrl("http://www.dedecms.com");
echo $hd->GetHtt() ;
//若儲存為檔案則以 $hd->SaveBin("dede.html");
$hd->Close();
取得http請求頭用
$hd- >GetHead("key")
設定請求頭
$hd->SetHead(key,value); (必須在呼叫 OpenUrl 之前設定)
程式碼如下:
/* ------------------------------------------------ ---------------------
//織夢Http下載類V1.0
//出自:織夢之旅 http://www .dedecms.com
//作者: IT柏拉圖
//時間: 2005-11-13 12:39
//聲明: 首發在落伍者網站,轉載請保留版權資訊
-
- -------------------------------------------------- ------------------ */
class DedeHttpDown
{
var $m_url = "";
🎜> var $m_scheme = "http";
var $m_host = var $m_user = "";
var $m_pass = "" ;
var $m_path = "/";
var $m_query var $m_error = "";
var $m_httphead = " " ;
var $m_html = "";
var var $HomeUrl = "";
var $JumpCount = 0 ;//防止多重重新導向陷入死循環
//
//初始化系統
🎜> {
") return ;
$urls = "";
$urls = @parse_url($url);
$this->m_url = $url;
if(is_array($urls))
{
if(!empty($urls["scheme"])) $this->m_scheme =$urls["scheme"])) $this->m_scheme =$urls["scheme"])) $this->m_scheme =$urls["scheme" ;
if(!empty($urls[" $this->m_user = $urls["user"];
if(!empty($urls["pass" $this->m_pass = $urls["pass"];
在}
)){
}
"path"])) $this->m_path = $urls["path"];
$this->m_urlpath = $this->m_path;
if(!empty($urls["query $this->m_query = $urls["query"];
}
rl = $urls["host"];
$this->BaseUrlPath = $this->HomeUrl.$urls["path"];
關於
$this->BaseUrlPath = ereg_replace("/$7,",$this->"",$07);
}
}
//
//開啟指定網址
{
//重新設定參數
;
$this->m_urlpath = "";
$this->m_scheme = "http";
$this->m_host = "";
$this->m_port =「80」;
$this->m_user = "";
$this->m_pass = "";
$this->m_path = "/";
$this->m_query = "";
$this->m_error = "";
$this->JumpCount = 0;
$this->m_httphead = Array();
//$this->m_puthead = "";
$this->m_html = "";
$this->Close();
//初始化系統
$this->PrivateStartSession();
}
//
//開啟303重新導向網址
{
//設定各參數
「」;
$this->m_urlpath = "";
$this->m_host = "";
$1 80”;
$this->m_user = "";
$this->m_path = "/";
";
$this->m_error = "";
$this->m_httphead = Array();
$this->m_html = $this->m_html = ""; $this->Close();
this->PrivateInit($url);
$this->PrivateStartSession();
🎜> //獲得某操作錯誤的原因
//
{ echo "錯誤訊息:".$this-> ;m_錯誤;
"特定返回頭:
";
foreach($this->m_httphead as $k=>$ $v
rn"; }
}
//
//在使用所有關於使用Get方法傳送的頭的答案結果是否正確
🎜> {
{ 如果( ereg("^ 2",$this->GetHead("http-state")) )
{ return
else
$this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."";
return false; //
//查看網頁上與網頁中網頁是否相同的選擇
🎜> {
if(ereg("^2",$ && eregi("^text",$this->GetHead("content-type" ))
{ return true; } return false;
}
}
🎜> //
function IsContentType($ctype)
ereg("^2",$this->GetHead("http-state"))
{ return true ; }
else
$this->m_error .= "類型不對 ".$this->GetHead("content-type")."
";
}
}
//
//用Http協議下載文件
//
function SaveToBin($savefilename)
{
if(!$this- >IsGetOK()) return false;
if(@feof($this->m_fp)) $this->m_error 已關閉!"; return false; }
$fp = fopen($savefilename,"w約->m_fp)){
fwrite($thisp,fread($this ->m_fp,1024));
}
fclose($fp);
🎜 > //儲存網頁內容為Text檔案
//
if($this->IsText()) $this->SaveBinFile($savefilename);
else return "";
p協定取得網頁的內容
//
function GetHtml()
if (!$this->IsText()) return "";
if($this->m_html!="") $this->m.com; if(!$this->m_fp||@ feof($this->m_fp)) return "";
while(!feof($this- $this->m_html .= fgets($this->m_fp,256);
}
return $this->m_html;
}
//
function PrivateStartSession()
{
$this->m_error .= "開啟遠端主機錯誤turn false;
}
if($this->GetHead("http-edition")=="HTTP/1.1") $httpv else $httpv = "HTTP/1.0";
//寄固定的起始請求頭GET、Host訊息
fputs($this->m_fp,"GET ".$ $this->m_puthead[" Host"] = $this->m_host;
//使用使用者自訂的請求頭
) { $this->m_puthead[" Accept"] = "*/*"; }
if(!isset($this->m_puthead[ 4.0 (compatible; MSIE 6.0; Windows NT 5.2)"; } 6.0; Windows NT 5.2)"; } 6.0; Windows NT 5.2)"; } http:/ /".$this->m_puthead["Host"]; }
foreach($this- $k = trim($k);
v = trim($v);
fputs($this->m_fp,"$k: $vrn");
}
//寄固定的結束請求頭
//HTTP1.1協定必須指定在文件結束後關閉連結,否則讀取文件時無法使用記憶中httpv=="HTTP/1.1") fputs($this->m_fp,"Connection: Closernrn");
//取得應答頭狀態資訊
$httpstas = explode(" ",fgets($this->m_fp,256)); "http-edition"] = trim($httpstas[0]);
$this->m_httphead["http-state"] = trim($httpstas[1]); for($i =2;$i
//取得詳細受試者中
$line = trim(fgets($this->m_fp,2) 56));
if($line == "") break;
$hkey = "";
$hvalue = "";
$v = 0;
for($i=0;$i if($line[$i]==":") $ v = 1; $hkey= trim($hkey); );
}
//判斷是否為3xx起始頭的回應
if(ereg("^ 3",$this->m_httphead["http-state" $this->JumpCount > 3) return;
if(isset($this- $newurl = $this->m_httphead["位置"];
$this->JumpOpenUrl($newurl);
}
$newurl = $this->FillUrl($newurl);
$this->JumpOpenUrl($newurl);
}
{ $this->m_error = "無法辨識的轉移應答!"; }
}//
} //
function GetHead($headname)
$headname = strtolower($headname);
if(isset($this->m_httphead[$headname]))
return $this->m_httphead[$headname];
else
return " ";
}
//
// function SetHead($skey,$svalue)
{
[$skey] = $svalue;
}
//
function PrivateOpenHost()
{
=="") return false;
$this->m_fp = @fsockopen($this->m_host,0$->m_fp = @fsockopen($this->m_host,0$-$-str. if(!$ this->m_fp){
$this->m_hor return false;
}
return true;
//
//關閉連線
//
close($this->m_fp);
}
//
//補全相網址
{
$i = 0;
$dstr = "";
$pstr = "";
$okurl = "";
$pathStep = 0;
$surl = trim($surl);
if($surl=="") return "";
$pos = strpos($surl,"#");
if($pos>0) $surl = substr($surl,0,$pos);
if($surl[0]=="/"){
$okurl = "http://".$this->HomeUrl."/".$surl;
}
else if($surl[0]==".")
{
else if($surl[0]=="/")
{
$okurl = "http://".$this->BaseUrlPath."/".substr($surl, 2 ,strlen($surl)-2);
}
其他{
$urls = explodeurl(
foreach($urls as $u){
if($u=="..") $pathStep++;
else if($i
$i++;
}
$urls = explode("/",$this->BaseUrlPath);
if(count($urls)
其他{
$pstr = "http://";
for($i=0;$i
$okurl = $pstr.$dstr;
}
}
}
el > $okurl = "http://".$- > BaseUrlPath ."/".$surl;
else if(strtolower(substr($surl,0,7))=="http://")
$okurl = $surl;
其他
$okurl = "http://".$this->BaseUrlPath."/".$surl;
}
$okurl = eregi_replace("^(http://)","",$okurl);
$okurl = eregi_replace("/{1,}","/",$okurl);
返回「http://」.$okurl;
}
}
?
以上就介紹了sock發布了一個用PHP fsockopen寫的HTTP下載的類,包括了sock方面的內容,希望對PHP教程有興趣的朋友得到幫助。