Home  >  Article  >  Backend Development  >  Publish a HTTP download class written with PHP fsockopen_PHP Tutorial

Publish a HTTP download class written with PHP fsockopen_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:18767browse

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 an image, it will be difficult to identify using the default function , but it is much simpler to judge by the HTTP response header. In addition, if the other party uses Refer to prevent hot links, 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 saving as a file, use $hd->SaveBin("dede.html");
$hd->Close();

Get the http request header with
$hd->GetHead("key")
Set the request header
$hd->SetHead(key, value); (must be called after OpenUrl Previously set)


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 = "";
var $m_error = "";
var $m_httphead = "";
var $m_html = "";
var $m_puthead = "";
var $BaseUrlPath = "";
var $HomeUrl = "";
var $JumpCount = 0;//Prevent multiple redirects from falling into an infinite loop
// Initialization system
//
function PrivateInit($url)
{
if($url=="") return ;
$urls = "";
$urls = @parse_url( $url);
$this->m_url = $url;         $this->m_host = $urls["host"];
If(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];

if(!empty($url s["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; }
                                                                                              Head("http-describe") ."
";
                                                                                                                           Whether it is text type
//
function IsText()
        {
                if(ereg("^2",$this->GetHead("http-state"))
                        && eregi("^text",$this->GetHead ("content-type")))
                                                                                                            $this->m_error .= "The content is non-text type or URL redirection
";
                         return false;                                        //                                                                        🎜> {
{ $ctype))
{ return true; }
else
{
$this- >m_error .= "Wrong type".$this->GetHead("content-type"). "
";
                                                                                                                                                      Download file
//
function SaveToBin($savefilename)
             { !"; return false; }
                                                                                                                                 fwrite($fp,fread($ this->m_fp,1024));                            return true; 🎜>                                                                                                              this->IsText()) $this- >SaveBinFile($savefilename);
function GetHtml()
             { if(!$this->m_fp||@feof($this->m_fp)) return ""; ;m_html .= fgets($this->m_fp,256);
                                                 m_html;
}
                                                                                                          !$this->PrivateOpenHost()){
$this->m_error .= "Error opening remote host!";
                                                                                                                                                                                                                                       

if($this->GetHead("http-edition")=="HTTP/1.1") $httpv = "HTTP/1.1";
else $httpv = "HTTP/1.0" ; > // Send a fixed start request head GET and HOST information
FPUTS ($ this- & gt; m_fp, "get". $ This- & gt; m_urlpath. "$ Httpvrn");                                                                                                                 { $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"]; }
foreach($this->m_puthead as $k =>$v){
                                                                                                 if($k!=""&&$v!="") {
                                                               fputs($this->m_fp,"$k: $vrn");                                    🎜>
//Send a fixed end request header
.1 The protocol must specify that the link should be closed after the document ends, otherwise feof cannot be used to determine the end when reading the document
if($httpv=="HTTP/1.1") fputs($this->m_fp,"Connection: Closernrn" );
            else fputs($this->m_fp,"rn"); ($this-> ;m_fp,256));
$this->m_httphead["http-edition"] = trim($httpstas[0]);
$this->m_httphead["http-state"] = trim($httpstas[1]);
               $this->m_httphead["http-describe"] =                                                                                      ){
                                                                                 $this->
                //获取详细应答头
                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
                                                                                                                                                                                                                                                                through"; }
                                                                                                                     name)
{
$headname = strtolower($headname);
if(isset($this->m_httphead[$headname]))
return $this->m_httphead[$headname];
          else
return "";
}
                                                                                                                                                                    🎜> $this- >m_puthead[$skey] = $svalue;
                                                                                          {
if($this ->m_host=="") return false;
                                                                                                                                                                                             🎜>                                                                                                                                                                                                      >                                                                                              @fclose($this->m_fp);
}
        //
        //补全相对网址
        //
        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;
  }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/317890.htmlTechArticle如果支持打开远程内容的选项的话,实际上php用fopen或file_get_contents都能获得一个网页的内容,但是默认的函数有个不足的地方就是无法获取...
Statement:
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