发布一个用PHP,带详细注释

 必赢亚州手机网站     |      2019-11-23 01:22

就算援救展开远程内容的选用的话,实际上php用fopen或file_get_contents都能赢得二个网页的剧情,然而暗中认可的函数有个不足的地点正是爱莫能助获得HTTP头,那在部分例外的采纳中非常不便于,如,有三个链接: 假使它回到的是二个图形,用暗中认可函数就很难分辨,但借使经过HTTP应答头来判别就归纳多了,此外假诺对方通过Refer来防盗链的话,也是力不能够及赢得的,用HTTP类就能够到家消除这么些标题,並且速度也差不离。 使用情势: $hd=new德德HttpDown(); $hd->OpenUrl(""); echo$hd->GetHtml(); //假若保存为文件则用$hd->SaveBin; $hd->Close(); 得到http乞请头用 $hd->GetHead 设置乞求头 $hd->SetHead; 代码如下: /*--------------------------------------------------------------------- //织梦Http下载类V1.0 //出自:织梦之旅 //小编:ITPlato //时间:2006-11-1312:39 //评释:首发在落伍者网址,转发请保留版权信息---------------------------------------------------------------------*/ classDedeHttpDown { var$m_url=""; var$m_urlpath=""; var$m_scheme="http"; var$m_host=""; var$m_port="80"; var$m_user=""; var$m_pass=""; var$m_path="/"; var$m_query=""; var$m_fp=""; var$m_error=""; var$m_httphead=""; var$m_html=""; var$m_puthead=""; var$BaseUrlPath=""; var$HomeUrl=""; var$JumpCount=0;//幸免多种重定向陷入死循环 // //起始化系统 // functionPrivateInitreturn; $urls=""; $urls=@parse_url; $this->m_url=$url; if { $this->m_host=$urls["host"]; if(!empty$this->m_scheme=$urls["scheme"]; if){ $this->m_user=$urls["user"]; } if){ $this->m_pass=$urls["pass"]; } if){ $this->m_port=$urls["port"]; } if)$this->m_path=$urls["path"]; $this->m_urlpath=$this->m_path; if(!empty{ $this->m_query=$urls["query"]; $this->m_urlpath.="?".$this->m_query; } $this->HomeUrl=$urls["host"]; $this->BaseUrlPath=$this->HomeUrl.$urls["path"]; $this->BaseUrlPath=ereg_replace$","/",$this->BaseUrlPath); $this->BaseUrlPath=ereg_replace("/$","",$this->BaseUrlPath); } } // //张开内定网站 // functionOpenUrl { //重设各参数 $this->m_url=""; $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->PrivateInit; $this->PrivateStartSession(); } // //张开303重定向网站 // functionJumpOpenUrl { //重设各参数 $this->m_url=""; $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++; $this->m_httphead=Array(); $this->m_html=""; $this->Close(); //初阶化系统 $this->PrivateInit; $this->PrivateStartSession(); } // //获得某操作错误的原由 // functionprintError() { echo"错误音信:".$this->m_error; echo"具体重回头:
"; foreach($this->m_httpheadas$k=>$v) {echo"$k=>$v
rn";} } // //判断用Get方法发送的头的答复结果是或不是科学 // functionIsGetOK() { if(ereg("^2",$this->GetHead {returntrue;} else { $this->m_error.=$this->GetHead."-".$this->GetHead."
"; returnfalse; } } // //看看再次来到的网页是或不是是text类型 // functionIsText() { if(ereg("^2",$this->GetHead &&eregi("^text",$this->GetHead {returntrue;} else { $this->m_error.="内容为非文本类型或网址重定向
"; returnfalse; } } // //剖断重回的网页是或不是是特定的花色 // functionIsContentType { if(ereg("^2",$this->GetHead &&$this->GetHead==strtolower {returntrue;} else { $this->m_error.="类型不对".$this->GetHead."
"; returnfalse; } } // //用Http合同下载文件 // functionSaveToBin { ifreturnfalse; if{$this->m_error="连接已经倒闭!";returnfalse;} $fp=fopen; while{ fwrite($fp,fread; } fclose; fclose; returntrue; } // //保存网页内容为Text文件 // functionSaveToText { if$this->SaveBinFile; elsereturn""; } // //用Http合同获得三个网页的内容 // functionGetHtml() { ifreturn""; ifreturn$this->m_html; if(!$this->m_fp||@feofreturn""; while{ $this->m_html.=fgets; } @fclose; return$this->m_html; } // //开始HTTP会话 // functionPrivateStartSession() { if(!$this->PrivateOpenHost{ $this->m_error.="张开远程主机出错!"; returnfalse; } if($this->GetHead=="HTTP/1.1")$httpv="HTTP/1.1"; else$httpv="HTTP/1.0"; //发送固定的原初伏乞头GET、Host信息fputs($this->m_fp,"GET".$this->m_urlpath."$httpvrn"); $this->m_puthead["Host"]=$this->m_host; //发送顾客自定义的乞请头 if(!isset($this->m_puthead["Accept"])){$this->m_puthead["Accept"]="*/*";} if(!isset($this->m_puthead["User-Agent"])){$this->m_puthead["User-Agent"]="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.2)";} if(!isset($this->m_puthead["Refer"])){$this->m_puthead["Refer"]=" foreach($this->m_putheadas$k=>$v){ $k=trim; if{ fputs($this->m_fp,"$k:$vrn"); } } //发送固定的利落央求头 //HTTP1.1商事必得内定文书档案结束后关门链接,不然读取文档时无计可施采用feof剖断截止iffputs($this->m_fp,"Connection:Closernrn"); elsefputs; //获取应答头状态新闻 $httpstas=explode("",fgets; $this->m_httphead["http-edition"]=trim; $this->m_httphead["http-state"]=trim; $this->m_httphead["http-describe"]=""; for($i=2;$im_httphead["http-describe"].="".trim; } //获取详细应答头 while{ $line=trim(fgets; ifbreak; $hkey=""; $hvalue=""; $v=0; for;$i++){ if$hvalue.=$line[$i]; if$v=1; if$hkey.=$line[$i]; } $hkey=trim$this->m_httphead[strtolower; } //决断是或不是是3xx上马的应答 if(ereg("^3",$this->m_httphead["http-state"])) { ifreturn; if(isset($this->m_httphead["location"])){ $newurl=$this->m_httphead["location"]; if(eregi{ $this->JumpOpenUrl; } else{ $newurl=$this->FillUrl; $this->JumpOpenUrl; } } else {$this->m_error="无法辨认的改变应答!";} }// } // //拿到多个Http头的值 // functionGetHead { $headname=strtolower; if(isset($this->m_httphead[$headname])) return$this->m_httphead[$headname]; else return""; } // //设置Http头的值 // functionSetHead { $this->m_puthead[$skey]=$svalue; } // //展开连接 // functionPrivateOpenHost() { ifreturnfalse; $this->m_fp=@fsockopen($this->m_host,$this->m_port,&$errno,&$errstr,10); if{ $this->m_error=$errstr; returnfalse; } else{ returntrue; } } // //关闭连接 // functionClose(){ @fclose; } // //补全相对网站 // functionFillUrl { $i=0; $dstr=""; $pstr=""; $okurl=""; $pathStep=0; $surl=trimreturn""; $pos=strpos; if$surl=substr; if{ $okurl="; } elseif { ifreturn""; elseif { $okurl="); } else{ $urls=explode; foreach{ if$pathStep++; elseif$dstr.=$urls[$i]."/"; else$dstr.=$urls[$i]; $i++; } $urls=explode("/",$this->BaseUrlPath); if<=$pathStep) return""; else{ $pstr="http://"; for-$pathStep;$i++) {$pstr.=$urls[$i]."/";} $okurl=$pstr.$dstr; } } } else { if $okurl="; elseif(strtolower=="http://") $okurl=$surl; else $okurl="; } $okurl=eregi_replace","",$okurl); $okurl=eregi_replace; return"; } } ?>

程序首假诺选取 HTTP 左券下载文件,HTTP1.1合计必需钦命文书档案截至后关闭链接,不然读取文书档案时不能利用feof判定甘休,能够有二种选拔办法,具体请下载查看源码。

 代码如下

<?php
/**
 * 下载远程文件类援助断点续传
 */
class HttpDownload {
    private $m_url = "";
     private $m_urlpath = "";
     private $m_scheme = "http";
     private $m_host = "";
     private $m_port = "80";
     private $m_user = "";
     private $m_pass = "";
     private $m_path = "/";
     private $m_query = "";
     private $m_fp = "";
     private $m_error = "";
    private $m_httphead = "" ;
    private $m_html = "";
 
    /**
     * 初始化
     */
    public function PrivateInit($url){
        $urls = "";
        $urls = @parse_url($url);
        $this->m_url = $url;
        if(is_array($urls)) {
            $this->m_host = $urls["host"];
            if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
            if(!empty($urls["user"])) $this->m_user = $urls["user"];
            if(!empty($urls["pass"])) $this->m_pass = $urls["pass"];
            if(!empty($urls["port"])) $this->m_port = $urls["port"];
            if(!empty($urls["path"])) $this->m_path = $urls["path"];
            $this->m_urlpath = $this->m_path;
            if(!empty($urls["query"])) {
                 $this->m_query = $urls["query"];
                 $this->m_urlpath .= "?".$this->m_query;
             }
          }
    }
 
    /**
    * 打开钦定网站
    */
    function OpenUrl($url) {
        #重设各参数
        $this->m_url = "";
        $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->m_httphead = "" ;
        $this->m_html = "";
        $this->Close();
        #初阶化系统
        $this->PrivateInit($url);
        $this->PrivateStartSession();
    }

    /**
    * 获得某操作不当的缘故
    */
    public function printError() {
        echo "错误音讯:".$this->m_error;
        echo "具体重回头:<br>";
        foreach($this->m_httphead as $k=>$v) {
            echo "$k => $v <br>rn";
        }
    }
 
    /**
    * 决断用Get方法发送的头的回复结果是不是正确
    */
    public function IsGetOK() {
        if( ereg("^2",$this->GetHead("http-state")) ) {
            return true;
        } else {
            $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br>";
            return false;
        }
    }
   
    /**
    * 看看再次来到的网页是或不是是text类型
    */
    public function IsText() {
        if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {
            return true;
        } else {
            $this->m_error .= "内容为非文本类型<br>";
            return false;
        }
    }
    /**
    * 判别重临的网页是或不是是特定的档期的顺序
    */
    public function IsContentType($ctype) {
        if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {
            return true;
        } else {
            $this->m_error .= "类型不对 ".$this->GetHead("content-type")."<br>";
            return false;
        }
    }
   
    /**
    * 用 HTTP 公约下载文件
    */
    public function SaveToBin($savefilename) {
        if (!$this->IsGetOK()) return false;
        if (@feof($this->m_fp)) {
            $this->m_error = "连接已经关闭!";
            return false;
        }
        $fp = fopen($savefilename,"w") or die("写入文件 $savefilename 失利!");
        while (!feof($this->m_fp)) {
            @fwrite($fp,fgets($this->m_fp,256));
        }
        @fclose($this->m_fp);
        return true;
    }
   
    /**
    * 保存网页内容为 Text 文件
    */
    public function SaveToText($savefilename) {
        if ($this->IsText()) {
            $this->SaveBinFile($savefilename);
        } else {
            return "";
        }
    }
   
    /**
    * 用 HTTP 协议获得三个网页的剧情
    */
    public function GetHtml() {
        if (!$this->IsText()) return "";
        if ($this->m_html!="") return $this->m_html;
        if (!$this->m_fp||@feof($this->m_fp)) return "";
        while(!feof($this->m_fp)) {
            $this->m_html .= fgets($this->m_fp,256);
        }
        @fclose($this->m_fp);
        return $this->m_html;
    }
   
    /**
    * 开始 HTTP 会话
    */
    public function PrivateStartSession() {
        if (!$this->PrivateOpenHost()) {
            $this->m_error .= "展开远程主机出错!";
            return false;
        }
        if ($this->GetHead("http-edition")=="HTTP/1.1") {
            $httpv = "HTTP/1.1";
        } else {
            $httpv = "HTTP/1.0";
        }
        fputs($this->m_fp,"GET ".$this->m_urlpath." $httpvrn");
        fputs($this->m_fp,"Host: ".$this->m_host."rn");
        fputs($this->m_fp,"Accept: */*rn");
        fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)rn");
        #HTTP1.1合计必需内定文档甘休后关门链接,不然读取文书档案时爱莫能助接纳feof判别甘休
        if ($httpv=="HTTP/1.1") {
            fputs($this->m_fp,"Connection: Closernrn");
        } else {
            fputs($this->m_fp,"rn");
        }
        $httpstas = fgets($this->m_fp,256);
        $httpstas = split(" ",$httpstas);
        $this->m_httphead["http-edition"] = trim($httpstas[0]);
        $this->m_httphead["http-state"] = trim($httpstas[1]);
        $this->m_httphead["http-describe"] = "";
        for ($i=2;$i<count($httpstas);$i++) {
            $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);
        }
        while (!feof($this->m_fp)) {
            $line = str_replace(""","",trim(fgets($this->m_fp,256)));
            if($line == "") break;
            if (ereg(":",$line)) {
                $lines = split(":",$line);
                $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);
            }
        }
    }
   
    /**
    * 得到多少个Http头的值
    */
    public function GetHead($headname) {
        $headname = strtolower($headname);
        if (isset($this->m_httphead[$headname])) {
            return $this->m_httphead[$headname];
        } else {
            return "";
        }
    }
   
    /**
    * 展开连接
    */
    public function PrivateOpenHost() {
        if ($this->m_host=="") return false;
        $this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10);
        if (!$this->m_fp){
            $this->m_error = $errstr;
            return false;
        } else {
            return true;
        }
    }
   
    /**
    * 关闭连接
    */
    public function Close(){
        @fclose($this->m_fp);
    }
}