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 相关文章推荐
简单分析ucenter 会员同步登录通信原理
Aug 25 PHP
用php来限制每个ip每天浏览页面数量的实现思路
Feb 24 PHP
详解PHP中的状态模式编程
Aug 11 PHP
PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码
Mar 25 PHP
php微信开发自定义菜单
Aug 27 PHP
Yii框架批量插入数据扩展类的简单实现方法
May 23 PHP
PHP实践教程之过滤、验证、转义与密码详解
Jul 24 PHP
利用PHP实现开心消消乐的算法示例
Oct 12 PHP
PHP操作Redis常用技巧总结
Apr 24 PHP
关于php unset对json_encode的影响详解
Nov 14 PHP
PHP实现批量修改文件名的方法示例
Sep 18 PHP
Thinkphp5.0框架的Db操作实例分析【连接、增删改查、链式操作等】
Oct 11 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
《破坏领主》销量已超100万 未来将继续开发新内容
2020/03/08 其他游戏
php中iconv函数使用方法
2008/05/24 PHP
php 验证码制作(网树注释思想)
2009/07/20 PHP
php图片加中文水印实现代码分享
2012/10/31 PHP
关于查看MSSQL 数据库 用户每个表 占用的空间大小
2013/06/21 PHP
PHP数据库操作之基于Mysqli的数据库操作类库
2014/04/19 PHP
CI配置多数据库访问的方法
2016/03/28 PHP
thinkphp框架类库扩展操作示例
2019/11/26 PHP
jQuery事件绑定.on()简要概述及应用
2013/02/07 Javascript
JavaScript 操作table,可以新增行和列并且隔一行换背景色代码分享
2013/07/05 Javascript
javascript与有限状态机详解
2014/05/08 Javascript
js实现的标题栏新消息闪烁提示效果
2014/06/06 Javascript
微信小程序  audio音频播放详解及实例
2016/11/02 Javascript
JS克隆,属性,数组,对象,函数实例分析
2016/11/26 Javascript
vue 父组件调用子组件方法及事件
2018/03/29 Javascript
bootstrap table表格插件之服务器端分页实例代码
2018/09/12 Javascript
layui表格 列自动适应大小失效的解决方法
2019/09/06 Javascript
解决layer.open后laydate失效的问题
2019/09/06 Javascript
小程序的上传文件接口的注意要点解析
2019/09/17 Javascript
js如何验证密码强度
2020/03/18 Javascript
react ant Design手动设置表单的值操作
2020/10/31 Javascript
git使用.gitignore设置不生效或不起作用问题的解决方法
2017/06/01 Python
django反向解析和正向解析的方式
2018/06/05 Python
pygame游戏之旅 python和pygame安装教程
2018/11/20 Python
Python设计模式之简单工厂模式实例详解
2019/01/22 Python
python中dict使用方法详解
2019/07/17 Python
Python异常处理机制结构实例解析
2020/07/23 Python
HTML5有哪些新特征
2015/12/01 HTML / CSS
挪威手表购物网站:Klokker
2016/09/19 全球购物
什么是事务?事务有哪些性质?
2012/03/11 面试题
男女朋友协议书
2014/04/23 职场文书
安全演讲稿开场白
2014/08/25 职场文书
大学生学习计划书
2014/09/15 职场文书
中学生2014国庆节演讲稿:不屈的民族
2014/09/21 职场文书
七年级写作指导之游记作文
2019/10/07 职场文书
Redis+Lua脚本实现计数器接口防刷功能(升级版)
2022/02/12 Redis