Home > Backend Development > PHP Tutorial > sock publishes an HTTP download class written in PHP fsockopen

sock publishes an HTTP download class written in PHP fsockopen

WBOY
Release: 2016-07-29 08:36:27
Original
942 people have browsed it

If the option to open remote content is supported, PHP can actually obtain the content of a web page using fopen or file_get_contents. However, the default function has a shortcoming that it cannot obtain the HTTP header, which is very inconvenient in some special applications. For example, there is a link:
http://www.abc.com/showvd.asp?id=18
If it returns a picture, it will be difficult to identify using the default function, but if it is judged through the HTTP response header It is much simpler. In addition, if the other party uses Refer to prevent hotlinking, it will not be obtained. Using the HTTP class can perfectly solve these problems, and the speed is almost the same.
Usage:
$hd = new DedeHttpDown();
$hd->OpenUrl("http://www.dedecms.com");
echo $hd->GetHtml();
//If saved For files, use $hd->SaveBin("dede.html");
$hd->Close();
To get the http request header, use
$hd->GetHead("key")
Set the request header
$hd->SetHead(key,value); (must be set before calling OpenUrl)
The code is as follows:
/* ------------------ -------------------------------------------------- --
//Dream Weaving HTTP Download Class V1.0
//From: Dream Weaving Journey http://www.dedecms.com
//Author: IT Plato
//Time: 2005-11-13 12: 39
//Statement: First published on the Left Behind website, please retain the copyright information when reprinting
-------------------------------- ------------------------------------- */
class DedeHttpDown
{
  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                                          = "" httphead = "" ;
var $m_html = "" ; $ Var $ m_puthead = "" ";
var $ baseurlpath =" "";
var $ homeurl = "" ";
var $ jumpcount = 0; // Prevent multiple heavy orientation from falling into a dead cycle
// The initialization system
//
                                                                                                                                                                                   
                        $urls = @parse_url($url); url;
                                                                                                       If(!empty($urls["scheme"])) $this->m_scheme = $ Urls ["scheme"];
if (! Empty ($ urls ["user"]) {
$ this- & gt; 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;
                          }
                          $this->HomeUrl = $urls["host"];
                          $this->BaseUrlPath = $this->HomeUrl.$urls["path"];
                          $this->BaseUrlPath = ereg_replace("/([^/]*).(.*)$","/",$this->BaseUrlPath);
                          $this->BaseUrlPath = ereg_replace("/$","",$this->BaseUrlPath);
                 }
        }
        //
        //打开指定网址
        //
        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->JumpCount = 0;
                $this->m_httphead = Array() ;
                //$this->m_puthead = "";
                $this->m_html = "";
                $this->Close();
                //初始化系统
                $this->PrivateInit($url);
                $this->PrivateStartSession();
        }
        //
        //打开303重定向网址
        //
        function JumpOpenUrl($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->JumpCount++;
                $this->m_httphead = Array() ;
                $this->m_html = "";
                $this->Close();
                //初始化系统
                $this->PrivateInit($url);
                $this->PrivateStartSession();
        }
        //
        //获得某操作错误的原因
        //
        function printError()
        {
                echo "错误信息:".$this->m_error;
                echo "具体返回头:
";
                foreach($this->m_httphead as $k=>$v)
                { echo "$k => $v 
rn"; }
        }
        //
        //判别用Get方法发送的头的应答结果是否正确
        //
        function IsGetOK()
        {
                if( ereg("^2",$this->GetHead("http-state")) )
                {        return true; }
                                                                                                                   "http-describe")."
";
                      return false;                                                                                                           //
                                                                                                                                                              GetHead("http-state"))
                                                                                                                                                                                                            .= "Content is non-text type or URL redirection
";
                                                                                                                                                                                                                        //Determine whether the returned web page is of a specific type
                                                                                                                                                                                                          )
                                                                                                                                                                                     )
                                                                                                                           = "Wrong type ".$this->GetHead("content-type")."
";
                                                                                                                                 GetHead ("                GetHead ("content-type"). " 无码}
                                                                                                       If(!$this->IsGetOK()) return false;
->m_fp)) { $this->m_error = "The connection has been closed!"; return false; }
                                                                                                                                                                            write($fp,fread($this->m_fp, 1024));
  
                                                                                                                                                                                                                                                  savefilename)
                                                                                                                          
                                                                                                                                                                                                              //Get the content of a web page using the HTTP protocol
//
                                                                                                                                                  this->m_html;
                                                                                           (!$this->m_fp||@feof($this->m_fp)) return "";
                                                                      ​fgets ($this->m_fp,256);
                                                                                                                                                                                                                                                                           ​PrivatestartSession () {
if (! $ This- & gt; privateopenhost ()) {
$ this- & gt; m_error. = "Open the remote host to go wrong!";
Return false;
}
            if($this->GetHead("http-edition")=="HTTP/1.1") $httpv                                                                                                                                                                                          $httpv                         //Send fixed Starting request header GET, Host information
                                                                                                                                                                                                            fputs($this->m_fp,"GET ".$this->m_urlpath." $httpvrn"); ->m_host;
                                                                                                                                                                                                                             . *"; }                                                                                                                                                                                                          if(!isset($this->m_puthead["User-Agent"])) { $this->m_puthead["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)"; }
                   if(!isset($this->m_puthead["Refer"])) { $this->m_puthead["Refer"] = "http://".$this-> ;m_puthead["Host"]; }
                                                                                                                                                                             ​                                                                                                                                                                     ($k!=""&&$v!=""){
                                                                                                                                                                                                                                                                                                                                                                                                      ​The /http1.1 protocol must be closed after the document is assigned, otherwise it is impossible to use Feof to judge the end when you read the document. ; fp,256));
                     $ this->m_httphead["http-edition"] = trim($httpstas[0]);
                                                                       >m_httphead["http-describe"] = "";
for($i=2;$i = " ".trim($httpstas[$i]);
                               
                //获取详细应答头
                while(!feof($this->m_fp)){
                        $line = trim(fgets($this->m_fp,256));
                        if($line == "") break;
                        $hkey = "";
                        $hvalue = "";
                        $v = 0;
                        for($i=0;$i                                if($v==1) $hvalue .= $line[$i];
                                if($line[$i]==":") $v = 1;
                                if($v==0) $hkey .= $line[$i];
                        }
                        $hkey = trim($hkey);
                        if($hkey!="") $this->m_httphead[strtolower($hkey)] = trim($hvalue);
                }
                //判断是否是3xx开头的应答
                if(ereg("^3",$this->m_httphead["http-state"]))
                {
                        if($this->JumpCount > 3) return;
                        if(isset($this->m_httphead["location"])){
                                $newurl = $this->m_httphead["location"];
                                if(eregi("^http",$newurl)){
                                        $this->JumpOpenUrl($newurl);
                                }
                                                                                                                                                                                                                                                                                else
                                                                                                    answer!"; }
                                                                                                                                                                                                                                                                                                                                                                                       ->m_httphead[$headname]))
                                                                                                                                                                                           
                                                                                                                                                                                                                                                      $skey,$svalue)
                                                                                                  { ;m_host=="") return false;
                                                                                                                                          $this-> $ this-& gt; m_fp) {
$ this-& gt; m_error = $ errtrstr;
return false; on close (){
                                                                                                       
        //
        //补全相对网址
        //
        function FillUrl($surl)
  {
    $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]==".")
    {
      if(strlen($surl)<=2) return "";
      else if($surl[0]=="/")
      {
              $okurl = "http://".$this->BaseUrlPath."/".substr($surl,2,strlen($surl)-2);
            }
      else{
        $urls = explode("/",$surl);
        foreach($urls as $u){
          if($u=="..") $pathStep++;
          else if($i          else $dstr .= $urls[$i];
          $i++;
        }
        $urls = explode("/",$this->BaseUrlPath);
        if(count($urls) <= $pathStep)
                return "";
        else{
          $pstr = "http://";
          for($i=0;$i          { $pstr .= $urls[$i]."/"; }
          $okurl = $pstr.$dstr;
        }
      }
    }
    else
    {
      if(strlen($surl)<7)
        $okurl = "http://".$this->BaseUrlPath."/".$surl;
      else if(strtolower(substr($surl,0,7))=="http://")
        $okurl = $surl;
      else
        $okurl = "http://".$this->BaseUrlPath."/".$surl;
    }
    $okurl = eregi_replace("^(http://)","",$okurl);
    $okurl = eregi_replace("/{1,}","/",$okurl);
    return "http://".$okurl;
  }
}
?>

以上就介绍了sock 发布一个用PHP fsockopen写的HTTP下载的类,包括了sock方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template