DedeHttpDown下载类

简介:

这个类的使用方法:
下载网页

Java代码   收藏代码
  1. <?php  
  2. $httpdown = new DedeHttpDown();  
  3. $httpdown->OpenUrl("http://www.dedecms.com");  
  4. echo $httpdown->GetHtml();  
  5.   
  6. //如果保存为文件则用 $hd->SaveBin("dede.html");  
  7. $httpdown->Close();  
  8. ?>  

如果下载图片 并保存,可以用

Java代码   收藏代码
  1. <?php  
  2. $httpdown = new DedeHttpDown();  
  3. $httpdown->OpenUrl("http://prato.bokele.com/0/0/399/bGluMi5qcGc=.jpg");  
  4. echo $httpdown->SaveBin("test.jpg");  
  5. $httpdown->Close();  
  6. echo "<img src='test.jpg'>";  
  7. ?>  

 DedeHttpDown

Java代码   收藏代码
  1. <?php  
  2. @set_time_limit(0);  
  3.   
  4. class DedeHttpDown  
  5. {  
  6.     var $m_url = '';  
  7.     var $m_urlpath = '';  
  8.     var $m_scheme = 'http';  
  9.     var $m_host = '';  
  10.     var $m_port = '80';  
  11.     var $m_user = '';  
  12.     var $m_pass = '';  
  13.     var $m_path = '/';  
  14.     var $m_query = '';  
  15.     var $m_fp = '';  
  16.     var $m_error = '';  
  17.     var $m_httphead = '';  
  18.     var $m_html = '';  
  19.     var $m_puthead = '';  
  20.     var $BaseUrlPath = '';  
  21.     var $HomeUrl = '';  
  22.     var $reTry = 0;  
  23.     var $JumpCount = 0;  
  24.   
  25.     //初始化系统  
  26.     function PrivateInit($url)  
  27.     {  
  28.         if($url=='') {  
  29.             return ;  
  30.         }  
  31.         $urls = '';  
  32.         $urls = @parse_url($url);  
  33.         $this->m_url = $url;  
  34.         if(is_array($urls))  
  35.         {  
  36.             $this->m_host = $urls["host"];  
  37.             if(!empty($urls["scheme"]))  
  38.             {  
  39.                 $this->m_scheme = $urls["scheme"];  
  40.             }  
  41.             if(!empty($urls["user"]))  
  42.             {  
  43.                 $this->m_user = $urls["user"];  
  44.             }  
  45.             if(!empty($urls["pass"]))  
  46.             {  
  47.                 $this->m_pass = $urls["pass"];  
  48.             }  
  49.             if(!empty($urls["port"]))  
  50.             {  
  51.                 $this->m_port = $urls["port"];  
  52.             }  
  53.             if(!empty($urls["path"]))  
  54.             {  
  55.                 $this->m_path = $urls["path"];  
  56.             }  
  57.             $this->m_urlpath = $this->m_path;  
  58.             if(!empty($urls["query"]))  
  59.             {  
  60.                 $this->m_query = $urls["query"];  
  61.                 $this->m_urlpath .= "?".$this->m_query;  
  62.             }  
  63.             $this->HomeUrl = $urls["host"];  
  64.             $this->BaseUrlPath = $this->HomeUrl.$urls["path"];  
  65.             $this->BaseUrlPath = preg_replace("/\/([^\/]*)\.(.*)$/","/",$this->BaseUrlPath);  
  66.             $this->BaseUrlPath = preg_replace("/\/$/","",$this->BaseUrlPath);  
  67.         }  
  68.     }  
  69.   
  70.     function ResetAny()  
  71.     {  
  72.         //重设各参数  
  73.         $this->m_url = "";  
  74.         $this->m_urlpath = "";  
  75.         $this->m_scheme = "http";  
  76.         $this->m_host = "";  
  77.         $this->m_port = "80";  
  78.         $this->m_user = "";  
  79.         $this->m_pass = "";  
  80.         $this->m_path = "/";  
  81.         $this->m_query = "";  
  82.         $this->m_error = "";  
  83.     }  
  84.   
  85.     //打开指定网址  
  86.     function OpenUrl($url,$requestType="GET")  
  87.     {  
  88.         $this->ResetAny();  
  89.         $this->JumpCount = 0;  
  90.         $this->m_httphead = Array() ;  
  91.         $this->m_html = '';  
  92.         $this->reTry = 0;  
  93.         $this->Close();  
  94.   
  95.         //初始化系统  
  96.         $this->PrivateInit($url);  
  97.         $this->PrivateStartSession($requestType);  
  98.     }  
  99.   
  100.     //转到303重定向网址  
  101.     function JumpOpenUrl($url)  
  102.     {  
  103.         $this->ResetAny();  
  104.         $this->JumpCount++;  
  105.         $this->m_httphead = Array() ;  
  106.         $this->m_html = "";  
  107.         $this->Close();  
  108.   
  109.         //初始化系统  
  110.         $this->PrivateInit($url);  
  111.         $this->PrivateStartSession('GET');  
  112.     }  
  113.   
  114.     //获得某操作错误的原因  
  115.     function printError()  
  116.     {  
  117.         echo "错误信息:".$this->m_error;  
  118.         echo "<br/>具体返回头:<br/>";  
  119.         foreach($this->m_httphead as $k=>$v){ echo "$k => $v <br/>\r\n"; }  
  120.     }  
  121.   
  122.     //判别用Get方法发送的头的应答结果是否正确  
  123.     function IsGetOK()  
  124.     {  
  125.         if( ereg("^2",$this->GetHead("http-state")) )  
  126.         {  
  127.             return true;  
  128.         }  
  129.         else  
  130.         {  
  131.             $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br/>";  
  132.             return false;  
  133.         }  
  134.     }  
  135.   
  136.     //看看返回的网页是否是text类型  
  137.     function IsText()  
  138.     {  
  139.         if( ereg("^2",$this->GetHead("http-state")) && eregi("text|xml",$this->GetHead("content-type")) )  
  140.         {  
  141.             return true;  
  142.         }  
  143.         else  
  144.         {  
  145.             $this->m_error .= "内容为非文本类型或网址重定向<br/>";  
  146.             return false;  
  147.         }  
  148.     }  
  149.   
  150.     //判断返回的网页是否是特定的类型  
  151.     function IsContentType($ctype)  
  152.     {  
  153.         if(ereg("^2",$this->GetHead("http-state"))  
  154.         && $this->GetHead("content-type")==strtolower($ctype))  
  155.         {   return true; }  
  156.         else  
  157.         {  
  158.             $this->m_error .= "类型不对 ".$this->GetHead("content-type")."<br/>";  
  159.             return false;  
  160.         }  
  161.     }  
  162.   
  163.     //用Http协议下载文件  
  164.     function SaveToBin($savefilename)  
  165.     {  
  166.         if(!$this->IsGetOK())  
  167.         {  
  168.             return false;  
  169.         }  
  170.         if(@feof($this->m_fp))  
  171.         {  
  172.             $this->m_error = "连接已经关闭!"return false;  
  173.         }  
  174.         $fp = fopen($savefilename,"w");  
  175.         while(!feof($this->m_fp))  
  176.         {  
  177.             fwrite($fp,fread($this->m_fp,1024));  
  178.         }  
  179.         fclose($this->m_fp);  
  180.         fclose($fp);  
  181.         return true;  
  182.     }  
  183.   
  184.     //保存网页内容为Text文件  
  185.     function SaveToText($savefilename)  
  186.     {  
  187.         if($this->IsText())  
  188.         {  
  189.             $this->SaveBinFile($savefilename);  
  190.         }  
  191.         else  
  192.         {  
  193.             return "";  
  194.         }  
  195.     }  
  196.   
  197.     //用Http协议获得一个网页的内容  
  198.     function GetHtml()  
  199.     {  
  200.         if(!$this->IsText())  
  201.         {  
  202.             return '';  
  203.         }  
  204.         if($this->m_html!='')  
  205.         {  
  206.             return $this->m_html;  
  207.         }  
  208.         if(!$this->m_fp||@feof($this->m_fp))  
  209.         {  
  210.             return '';  
  211.         }  
  212.         while(!feof($this->m_fp))  
  213.         {  
  214.             $this->m_html .= fgets($this->m_fp,256);  
  215.         }  
  216.         @fclose($this->m_fp);  
  217.         return $this->m_html;  
  218.     }  
  219.   
  220.     //开始HTTP会话  
  221.     function PrivateStartSession($requestType="GET")  
  222.     {  
  223.         if(!$this->PrivateOpenHost())  
  224.         {  
  225.             $this->m_error .= "打开远程主机出错!";  
  226.             return false;  
  227.         }  
  228.         $this->reTry++;  
  229.         if($this->GetHead("http-edition")=="HTTP/1.1")  
  230.         {  
  231.             $httpv = "HTTP/1.1";  
  232.         }  
  233.         else  
  234.         {  
  235.             $httpv = "HTTP/1.0";  
  236.         }  
  237.         $ps = explode('?',$this->m_urlpath);  
  238.   
  239.         $headString = '';  
  240.   
  241.         //发送固定的起始请求头GET、Host信息  
  242.         if($requestType=="GET")  
  243.         {  
  244.             $headString .= "GET ".$this->m_urlpath." $httpv\r\n";  
  245.         }  
  246.         else  
  247.         {  
  248.             $headString .= "POST ".$ps[0]." $httpv\r\n";  
  249.         }  
  250.         $this->m_puthead["Host"] = $this->m_host;  
  251.   
  252.         //发送用户自定义的请求头  
  253.         if(!isset($this->m_puthead["Accept"]))  
  254.         {  
  255.             $this->m_puthead["Accept"] = "*/*";  
  256.         }  
  257.         if(!isset($this->m_puthead["User-Agent"]))  
  258.         {  
  259.             $this->m_puthead["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)";  
  260.         }  
  261.         if(!isset($this->m_puthead["Refer"]))  
  262.         {  
  263.             $this->m_puthead["Refer"] = "http://".$this->m_puthead["Host"];  
  264.         }  
  265.   
  266.         foreach($this->m_puthead as $k=>$v)  
  267.         {  
  268.             $k = trim($k);  
  269.             $v = trim($v);  
  270.             if($k!=""&&$v!="")  
  271.             {  
  272.                 $headString .= "$k: $v\r\n";  
  273.             }  
  274.         }  
  275.         fputs($this->m_fp, $headString);  
  276.         if($requestType=="POST")  
  277.         {  
  278.             $postdata = "";  
  279.             if(count($ps)>1)  
  280.             {  
  281.                 for($i=1;$i<count($ps);$i++)  
  282.                 {  
  283.                     $postdata .= $ps[$i];  
  284.                 }  
  285.             }  
  286.             else  
  287.             {  
  288.                 $postdata = "OK";  
  289.             }  
  290.             $plen = strlen($postdata);  
  291.             fputs($this->m_fp,"Content-Type: application/x-www-form-urlencoded\r\n");  
  292.             fputs($this->m_fp,"Content-Length: $plen\r\n");  
  293.         }  
  294.   
  295.         //发送固定的结束请求头  
  296.         //HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束  
  297.         if($httpv=="HTTP/1.1")  
  298.         {  
  299.             fputs($this->m_fp,"Connection: Close\r\n\r\n");  
  300.         }  
  301.         else  
  302.         {  
  303.             fputs($this->m_fp,"\r\n");  
  304.         }  
  305.         if($requestType=="POST")  
  306.         {  
  307.             fputs($this->m_fp,$postdata);  
  308.         }  
  309.   
  310.         //获取应答头状态信息  
  311.         $httpstas = explode(" ",fgets($this->m_fp,256));  
  312.         $this->m_httphead["http-edition"] = trim($httpstas[0]);  
  313.         $this->m_httphead["http-state"] = trim($httpstas[1]);  
  314.         $this->m_httphead["http-describe"] = "";  
  315.         for($i=2;$i<count($httpstas);$i++)  
  316.         {  
  317.             $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);  
  318.         }  
  319.   
  320.         //获取详细应答头  
  321.         while(!feof($this->m_fp))  
  322.         {  
  323.             $line = trim(fgets($this->m_fp,256));  
  324.             if($line == "")  
  325.             {  
  326.                 break;  
  327.             }  
  328.             $hkey = "";  
  329.             $hvalue = "";  
  330.             $v = 0;  
  331.             for($i=0;$i<strlen($line);$i++)  
  332.             {  
  333.                 if($v==1)  
  334.                 {  
  335.                     $hvalue .= $line[$i];  
  336.                 }  
  337.                 if($line[$i]==":")  
  338.                 {  
  339.                     $v = 1;  
  340.                 }  
  341.                 if($v==0)  
  342.                 {  
  343.                     $hkey .= $line[$i];  
  344.                 }  
  345.             }  
  346.             $hkey = trim($hkey);  
  347.             if($hkey!="")  
  348.             {  
  349.                 $this->m_httphead[strtolower($hkey)] = trim($hvalue);  
  350.             }  
  351.         }  
  352.   
  353.         //如果连接被不正常关闭,重试  
  354.         if(feof($this->m_fp))  
  355.         {  
  356.             if($this->reTry > 10)  
  357.             {  
  358.                 return false;  
  359.             }  
  360.             $this->PrivateStartSession($requestType);  
  361.         }  
  362.   
  363.         //判断是否是3xx开头的应答  
  364.         if(ereg("^3",$this->m_httphead["http-state"]))  
  365.         {  
  366.             if($this->JumpCount > 3)  
  367.             {  
  368.                 return;  
  369.             }  
  370.             if(isset($this->m_httphead["location"]))  
  371.             {  
  372.                 $newurl = $this->m_httphead["location"];  
  373.                 if(eregi("^http",$newurl))  
  374.                 {  
  375.                     $this->JumpOpenUrl($newurl);  
  376.                 }  
  377.                 else  
  378.                 {  
  379.                     $newurl = $this->FillUrl($newurl);  
  380.                     $this->JumpOpenUrl($newurl);  
  381.                 }  
  382.             }  
  383.             else  
  384.             {  
  385.                 $this->m_error = "无法识别的答复!";  
  386.             }  
  387.         }  
  388.     }  
  389.   
  390.     //获得一个Http头的值  
  391.     function GetHead($headname)  
  392.     {  
  393.         $headname = strtolower($headname);  
  394.         return isset($this->m_httphead[$headname]) ? $this->m_httphead[$headname] : '';  
  395.     }  
  396.   
  397.     //设置Http头的值  
  398.     function SetHead($skey,$svalue)  
  399.     {  
  400.         $this->m_puthead[$skey] = $svalue;  
  401.     }  
  402.   
  403.     //打开连接  
  404.     function PrivateOpenHost()  
  405.     {  
  406.         if($this->m_host=="")  
  407.         {  
  408.             return false;  
  409.         }  
  410.         $errno = "";  
  411.         $errstr = "";  
  412.         $this->m_fp = @fsockopen($this->m_host, $this->m_port, $errno, $errstr,10);  
  413.         if(!$this->m_fp)  
  414.         {  
  415.             $this->m_error = $errstr;  
  416.             return false;  
  417.         }  
  418.         else  
  419.         {  
  420.             return true;  
  421.         }  
  422.     }  
  423.   
  424.     //关闭连接  
  425.     function Close()  
  426.     {  
  427.         @fclose($this->m_fp);  
  428.     }  
  429.   
  430.     //补全相对网址  
  431.     function FillUrl($surl)  
  432.     {  
  433.         $i = 0;  
  434.         $dstr = "";  
  435.         $pstr = "";  
  436.         $okurl = "";  
  437.         $pathStep = 0;  
  438.         $surl = trim($surl);  
  439.         if($surl=="")  
  440.         {  
  441.             return "";  
  442.         }  
  443.         $pos = strpos($surl,"#");  
  444.         if($pos>0)  
  445.         {  
  446.             $surl = substr($surl,0,$pos);  
  447.         }  
  448.         if($surl[0]=="/")  
  449.         {  
  450.             $okurl = "http://".$this->HomeUrl.$surl;  
  451.         }  
  452.         else if($surl[0]==".")  
  453.         {  
  454.             if(strlen($surl)<=1)  
  455.             {  
  456.                 return "";  
  457.             }  
  458.             else if($surl[1]=="/")  
  459.             {  
  460.                 $okurl = "http://".$this->BaseUrlPath."/".substr($surl,2,strlen($surl)-2);  
  461.             }  
  462.             else  
  463.             {  
  464.                 $urls = explode("/",$surl);  
  465.                 foreach($urls as $u)  
  466.                 {  
  467.                     if($u=="..")  
  468.                     {  
  469.                         $pathStep++;  
  470.                     }  
  471.                     else if($i<count($urls)-1)  
  472.                     {  
  473.                         $dstr .= $urls[$i]."/";  
  474.                     }  
  475.                     else  
  476.                     {  
  477.                         $dstr .= $urls[$i];  
  478.                     }  
  479.                     $i++;  
  480.                 }  
  481.                 $urls = explode("/",$this->BaseUrlPath);  
  482.                 if(count($urls) <= $pathStep)  
  483.                 {  
  484.                     return "";  
  485.                 }  
  486.                 else  
  487.                 {  
  488.                     $pstr = "http://";  
  489.                     for($i=0;$i<count($urls)-$pathStep;$i++)  
  490.                     {  
  491.                         $pstr .= $urls[$i]."/";  
  492.                     }  
  493.                     $okurl = $pstr.$dstr;  
  494.                 }  
  495.             }  
  496.         }  
  497.         else  
  498.         {  
  499.             if(strlen($surl)<7)  
  500.             {  
  501.                 $okurl = "http://".$this->BaseUrlPath."/".$surl;  
  502.             }  
  503.             else if(strtolower(substr($surl,0,7))=="http://")  
  504.             {  
  505.                 $okurl = $surl;  
  506.             }  
  507.             else  
  508.             {  
  509.                 $okurl = "http://".$this->BaseUrlPath."/".$surl;  
  510.             }  
  511.         }  
  512.         $okurl = eregi_replace("^(http://)","",$okurl);  
  513.         $okurl = eregi_replace("/{1,}","/",$okurl);  
  514.         return "http://".$okurl;  
  515.     }  
  516. }  
  517.   
  518. ?>  
相关文章
|
4月前
|
Java
Java【付诸实践 04】Jar包class文件反编译、修改、重新编译打包方法(含反编译工具jd-gui-windows-1.6.6.zip百度云资源)
Java【付诸实践 04】Jar包class文件反编译、修改、重新编译打包方法(含反编译工具jd-gui-windows-1.6.6.zip百度云资源)
149 0
|
7月前
|
IDE Java API
如何配置 jad,让 Eclipse 可以自动显示反编译之后的 .class 源代码
如何配置 jad,让 Eclipse 可以自动显示反编译之后的 .class 源代码
93 0
|
Java Android开发
哇!eclipse+webservice开发实例居然这样写(有源码可用)
哇!eclipse+webservice开发实例居然这样写(有源码可用)
107 0
哇!eclipse+webservice开发实例居然这样写(有源码可用)
|
Java 程序员
Java面向对象8——接口(内含IDEA中有关创建接口的创建说明)
上一次我们说了抽象方法的存在是因为父类可以抽取子类中的共性方法,但是共性方法内实现方法不同所以要将共性方法写成抽象方法,那么如果子类中有一个“另类”不需要这个方法的话,我们就无法使用抽象方法了,而是要使用接口,哪个子类需要用,就继承该接口
238 0
Java面向对象8——接口(内含IDEA中有关创建接口的创建说明)
idea运行一个类自动跳转到其他类
初学者在使用idea时候经常会遇到运行当前这个类自动跳转到其他类去运行。
423 0
|
资源调度
插件类……
插件类……
|
数据安全/隐私保护
【安装教程】【FormatFactory(格式工厂)】(附带安装包下载)
【安装教程】【FormatFactory(格式工厂)】(附带安装包下载)
424 0
【安装教程】【FormatFactory(格式工厂)】(附带安装包下载)
|
存储 前端开发 JavaScript
JAVA文件下载方式和获取文件下载进度方式
JAVA文件下载方式和获取文件下载进度方式
650 0
|
Java Android开发
使用JAD集成到Eclipse里去,方便地查看任意Java类的源代码
使用JAD集成到Eclipse里去,方便地查看任意Java类的源代码
100 0
使用JAD集成到Eclipse里去,方便地查看任意Java类的源代码
|
Python 索引
python基础教程:包的创建及导入
包是一种通过用“带点号的模块名”来构造 Python 模块命名空间的方法。 例如,模块名 A.B 表示 A 包中名为 B 的子模块。正如模块的使用使得不同模块的作者不必担心彼此的全局变量名称一样,使用加点的模块名可以使得 NumPy 或 Pillow 等多模块软件包的作者不必担心彼此的模块名称一样。