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中MVC模式的模板引擎开发经验分享
Mar 23 PHP
百度ping方法使用示例 自动ping百度
Jan 26 PHP
PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径
Jun 30 PHP
浅析PHP中strlen和mb_strlen的区别
Aug 31 PHP
php实现mysql事务处理的方法
Dec 25 PHP
smarty内置函数config_load用法实例
Jan 22 PHP
php检查日期函数checkdate用法实例
Mar 19 PHP
Joomla实现组件中弹出一个模式(modal)窗口的方法
May 04 PHP
关于PHP文件的自动运行方法分析
May 13 PHP
在Laravel中使用DataTables插件的方法
May 29 PHP
实例解析php的数据类型
Oct 24 PHP
PHP asXML()函数讲解
Feb 03 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错误级别的设置方法
2013/06/17 PHP
PHP实现十进制、二进制、八进制和十六进制转换相关函数用法分析
2017/04/25 PHP
thinkphp5实现无限级分类
2019/02/18 PHP
php-7.3.6 编译安装过程
2020/02/11 PHP
PHP number_format函数原理及实例解析
2020/07/14 PHP
让广告代码不再影响你的网页加载速度
2006/07/07 Javascript
jQuery选择器简明总结(含用法实例,一目了然)
2014/04/25 Javascript
[原创]推荐10款最热门jQuery UI框架
2014/08/19 Javascript
jQuery源码分析之jQuery中的循环技巧详解
2014/09/06 Javascript
jquery实现的回旋滚动效果完整实例【附demo源码下载】
2016/09/20 Javascript
详解Vue-cli中的静态资源管理(src/assets和static/的区别)
2018/06/19 Javascript
Bootstrap fileinput 上传新文件移除时触发服务器同步删除的配置
2018/10/08 Javascript
关于vue3默认把所有onSomething当作v-on事件绑定的思考
2020/05/15 Javascript
[00:52]黑暗之门更新 新英雄孽主驾临DOTA2
2016/08/24 DOTA
[00:36]TI7不朽珍藏III——斯温不朽展示
2017/07/15 DOTA
Python简单实现TCP包发送十六进制数据的方法
2016/04/16 Python
Python+matplotlib+numpy实现在不同平面的二维条形图
2018/01/02 Python
python使用python-pptx删除ppt某页实例
2020/02/14 Python
python 实现rolling和apply函数的向下取值操作
2020/06/08 Python
基于Jquery和Css3代码制作可以缩放的搜索框
2015/11/19 HTML / CSS
html5基础标签(html5视频标签 html5新标签用法)
2013/12/30 HTML / CSS
canvas探照灯效果的示例代码
2018/11/30 HTML / CSS
高二历史教学反思
2014/01/25 职场文书
服装行业创业计划书范文
2014/02/05 职场文书
公司管理建议书范文
2014/03/12 职场文书
企业出纳岗位职责
2014/03/12 职场文书
石油工程专业毕业生求职信
2014/04/13 职场文书
专业技术职务聘任证明
2015/03/02 职场文书
家长意见和建议怎么写
2015/06/04 职场文书
车间班组长竞聘书
2015/09/15 职场文书
简历自我评价:教师师德表现自我评价
2019/04/24 职场文书
干货:企业内部人才推荐奖励方案!
2019/07/09 职场文书
创业计划书之暑假培训班
2019/11/09 职场文书
创业计划书之酒吧
2019/12/02 职场文书
导游词之南京夫子庙
2019/12/09 职场文书
win10识别不了U盘怎么办 win10系统读取U盘失败的解决办法
2022/08/05 数码科技