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

Publish a HTTP download class written with PHP fsockopen_PHP Tutorial

Jul 21, 2016 pm 03:57 PM
fsockopen http php download content release Open support use of kind Remotely Options

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles