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 02 PHP
PHP 图片上传实现代码 带详细注释
Apr 29 PHP
关于使用coreseek并为其做分页的介绍
Jun 21 PHP
php+memcache实现的网站在线人数统计代码
Jul 04 PHP
php命令行用法入门实例教程
Oct 27 PHP
PHP多个文件上传到服务器实例
Oct 29 PHP
PHP查找数值数组中不重复最大和最小的10个数的方法
Apr 20 PHP
php防止用户重复提交表单
Nov 02 PHP
PHP封装的字符串加密解密函数
Dec 18 PHP
AJAX的使用方法详解
Apr 29 PHP
PHP7扩展开发之基于函数方式使用lib库的方法详解
Jan 15 PHP
使用PHPWord生成word文档的方法详解
Jun 06 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
弄了个检测传输的参数是否为数字的Function
2006/12/06 PHP
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
2014/06/23 PHP
Chrome Web App开发小结
2014/09/04 PHP
PHP的文件操作与算法实现的面试题示例
2015/08/10 PHP
Eclipse PHPEclipse 配置的具体步骤
2017/08/08 PHP
Laravel使用scout集成elasticsearch做全文搜索的实现方法
2018/11/30 PHP
PHP利用DWZ.CN服务生成短网址
2019/08/11 PHP
JavaScript实现x秒后自动跳转到一个页面
2013/01/03 Javascript
关于JavaScript对象的动态选择及遍历对象
2014/03/10 Javascript
js获取微信版本号的方法
2015/05/12 Javascript
分享javascript实现的冒泡排序代码并优化
2016/06/05 Javascript
解决vue页面DOM操作不生效的问题
2018/03/17 Javascript
vue实现选项卡及选项卡切换效果
2018/04/24 Javascript
Vue隐藏显示、只读实例代码
2018/07/18 Javascript
Vue中的transition封装组件的实现方法
2019/08/13 Javascript
Moment.js实现多个同时倒计时
2019/08/26 Javascript
vue表单数据交互提交演示教程
2019/11/13 Javascript
在python中的socket模块使用代理实例
2014/05/29 Python
在django中使用自定义标签实现分页功能
2017/07/04 Python
Python实现获取邮箱内容并解析的方法示例
2018/06/16 Python
解决python3 Pycharm上连接数据库时报错的问题
2018/12/03 Python
Python list和str互转的实现示例
2020/11/16 Python
HTML5 Web存储方式的localStorage和sessionStorage进行数据本地存储案例应用
2012/12/09 HTML / CSS
Joules美国官网:出色的英国风格
2017/10/30 全球购物
预订奥兰多和佛罗里达州公园门票:FloridaTix
2018/01/03 全球购物
Araks官网:纽约内衣品牌
2020/10/15 全球购物
Juice Beauty官网:有机美容产品,护肤与化妆品
2020/06/13 全球购物
Cynthia Rowley官网:全球领先的生活方式品牌
2020/10/27 全球购物
介绍一下SQL Server的全文索引
2013/08/15 面试题
大学生物业管理求职信
2013/10/24 职场文书
九年级历史教学反思
2014/01/27 职场文书
ktv筹备计划书
2014/05/03 职场文书
催款函怎么写
2015/06/24 职场文书
Spring Boot 实现敏感词及特殊字符过滤处理
2021/06/29 Java/Android
Pandas实现批量拆分与合并Excel的示例代码
2022/05/30 Python