php远程下载类分享


Posted in PHP onApril 13, 2016

本文实例为大家分享了php远程下载类,如下

<?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>\r\n"; 
} 
} 
 
/** 
* 判别用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." $httpv\r\n"); 
fputs($this->m_fp,"Host: ".$this->m_host."\r\n"); 
fputs($this->m_fp,"Accept: */*\r\n"); 
fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)\r\n"); 
#HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束 
if ($httpv=="HTTP/1.1") { 
fputs($this->m_fp,"Connection: Close\r\n\r\n"); 
} else { 
fputs($this->m_fp,"\r\n"); 
} 
$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); 
} 
} 
 
#两种使用方法,分别如下: 
 
#打开网页 
$httpdown = new HttpDownload(); 
$httpdown->OpenUrl("http://www.google.com.hk"); 
echo $httpdown->GetHtml(); 
$httpdown->Close(); 
 
 
#下载文件 
$file = new HttpDownload(); # 实例化类 
$file->OpenUrl("http://dldir1.qq.com/qqfile/qq/QQ8.2/17724/QQ8.2.exe"); # 远程文件地址 
$file->SaveToBin("qq.exe"); # 保存路径及文件名 
$file->Close(); # 释放资源

以上就是本文的全部内容,希望对大家的学习有所帮助。

PHP 相关文章推荐
php批量删除数据
Jan 18 PHP
PHP $_SERVER详解
Jan 16 PHP
php中定义网站根目录的常用方法
Aug 08 PHP
解析如何屏蔽php中的phpinfo()函数
Jun 06 PHP
PHP实现删除非站内外部链接实例代码
Jun 17 PHP
PHP的Json中文处理解决方案
Sep 29 PHP
详解PHP原生DOM对象操作XML的方法
Oct 17 PHP
Zend Framework入门教程之Zend_Registry组件用法详解
Dec 09 PHP
详解PHP序列化和反序列化原理
Jan 15 PHP
php 后端实现JWT认证方法示例
Sep 04 PHP
windows 2008r2+php5.6.28环境搭建详细过程
Jun 18 PHP
用php如何解决大文件分片上传问题
Jul 07 PHP
Thinkphp和onethink实现微信支付插件
Apr 13 #PHP
PHP MSSQL 分页实例
Apr 13 #PHP
php构造方法中析构方法在继承中的表现
Apr 12 #PHP
非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程
Apr 12 #PHP
ThinkPHP框架里隐藏index.php
Apr 12 #PHP
PHP 绘制网站登录首页图片验证码
Apr 12 #PHP
PHP中Restful api 错误提示返回值实现思路
Apr 12 #PHP
You might like
PHP随机生成随机个数的字母组合示例
2014/01/14 PHP
PHP生成条形码大揭秘
2015/09/24 PHP
PHP registerXPathNamespace()函数讲解
2019/02/03 PHP
PHP操作XML中XPath的应用示例
2019/07/04 PHP
jquery 简单的进度条实现代码
2010/03/11 Javascript
js实现的切换面板实例代码
2013/06/17 Javascript
Knockout visible绑定使用方法
2013/11/15 Javascript
按下Enter焦点移至下一个控件的实现js代码
2013/12/11 Javascript
javascript中parentNode,childNodes,children的应用详解
2013/12/17 Javascript
javascript解析json数据的3种方式
2014/05/08 Javascript
jQuery图片切换插件jquery.cycle.js使用示例
2014/06/16 Javascript
javascript常用函数(2)
2015/11/05 Javascript
node模块机制与异步处理详解
2016/03/13 Javascript
BootStrap的Datepicker控件使用心得分享
2016/05/25 Javascript
详解Vue中状态管理Vuex
2017/05/11 Javascript
基于mpvue的小程序项目搭建的步骤
2018/05/22 Javascript
jquery获取select选中值的文本,并赋值给另一个输入框的方法
2018/08/21 jQuery
jQuery实现带3D切割效果的轮播图功能示例【附源码下载】
2019/04/04 jQuery
一些手写JavaScript常用的函数汇总
2019/04/16 Javascript
Node 代理访问的实现
2019/09/19 Javascript
H5 js点击按钮复制文本到粘贴板
2020/11/19 Javascript
[01:05:40]VG vs Newbee 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
关于numpy.where()函数 返回值的解释
2019/12/06 Python
html5中canvas学习笔记2-判断浏览器是否支持canvas
2013/01/06 HTML / CSS
html5本地存储之localstorage 、本地数据库、sessionStorage简单使用示例
2014/05/08 HTML / CSS
联想墨西哥官方网站:Lenovo墨西哥
2016/08/17 全球购物
贝玲妃英国官网:Benefit英国
2018/02/03 全球购物
微软中国官方旗舰店:销售Surface、Xbox One、笔记本电脑、Office
2018/07/23 全球购物
法国最大的在线眼镜店:EasyLunettes
2019/08/26 全球购物
Myprotein荷兰官网:欧洲第一运动营养品牌
2020/07/11 全球购物
师范应届生教师求职信
2013/11/05 职场文书
2014的自我评价
2014/01/13 职场文书
爱岗敬业演讲稿范文
2014/01/14 职场文书
药学专业学生的自我评价分享
2014/02/06 职场文书
党的群众路线教育实践活动学习心得体会
2014/03/03 职场文书
劳动仲裁代理词范文
2015/05/25 职场文书