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 相关文章推荐
一个简单的自动发送邮件系统(一)
Oct 09 PHP
PHP 服务器配置(使用Apache及IIS两种方法)
Jun 01 PHP
php whois查询API制作方法
Jun 23 PHP
浅析Dos下运行php.exe,出现没有找到php_mbstring.dll 错误的解决方法
Jun 29 PHP
php批量删除数据库下指定前缀的表以prefix_为例
Aug 24 PHP
php隐藏IP地址后两位显示为星号的方法
Nov 21 PHP
PHP页面转UTF-8中文编码乱码的解决办法
Oct 20 PHP
php魔术方法功能与用法实例分析
Oct 19 PHP
PHP带节点操作的无限分类实现方法详解
Nov 09 PHP
解决安装WampServer时提示缺少msvcr110.dll文件的问题
Jul 09 PHP
Laravel如何使用Redis共享Session
Feb 23 PHP
PHP 爬取网页的主要方法
Jul 13 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生成静态HTML速度快类库
2007/03/18 PHP
用PHP为SHOPEX增加日志功能代码
2010/07/02 PHP
PHP获取当前页面完整URL的实现代码
2013/06/10 PHP
php使用curl抓取qq空间的访客信息示例
2014/02/28 PHP
PHP+Oracle本地开发环境搭建方法详解
2019/04/01 PHP
PHP MVC框架中类的自动加载机制实例分析
2019/09/18 PHP
javascript:以前写的xmlhttp池,代码
2008/05/18 Javascript
js浮点数保留两位小数点示例代码(四舍五入)
2013/12/26 Javascript
js判断60秒以及倒计时示例代码
2014/01/24 Javascript
jQuery+canvas实现的球体平抛及颜色动态变换效果
2016/01/28 Javascript
实践中学习AngularJS表单
2016/03/21 Javascript
简单实现bootstrap选项卡效果
2017/02/08 Javascript
引入外部js脚本加载慢与页面白屏问题的解决
2018/12/10 Javascript
使用jquery的cookie实现登录页记住用户名和密码的方法
2019/03/13 jQuery
如何根据业务封装自己的功能组件
2019/04/19 Javascript
three.js显示中文字体与tween应用详析
2021/01/04 Javascript
[04:45]DOTA2上海特级锦标赛主赛事第四日RECAP
2016/03/06 DOTA
[10:05]DOTA2-DPC中国联赛 正赛 iG vs PSG.LGD 选手采访
2021/03/11 DOTA
python消除序列的重复值并保持顺序不变的实例
2018/11/08 Python
Python爬虫抓取技术的一些经验
2019/07/12 Python
Django框架之DRF 基于mixins来封装的视图详解
2019/07/23 Python
解决python有时候import不了当前的包问题
2019/08/28 Python
使用python实现kNN分类算法
2019/10/16 Python
Python使用itcaht库实现微信自动收发消息功能
2020/07/13 Python
Selenium之模拟登录铁路12306的示例代码
2020/07/31 Python
CSS3条纹背景制作的实战攻略
2016/05/31 HTML / CSS
HTML5本地存储之IndexedDB
2017/06/16 HTML / CSS
澳大利亚时尚前卫设计师珠宝在线:Amber Sceats
2017/10/04 全球购物
Clarisonic美国官网:科莱丽声波洁面仪
2017/10/12 全球购物
机电专业毕业生推荐信
2013/11/10 职场文书
中秋节英文祝福语句(14句)
2019/09/11 职场文书
Mysql数据库手动及定时备份步骤
2021/11/07 MySQL
基于Python实现一个春节倒计时脚本
2022/01/22 Python
Spring Boot DevTools 全局配置学习指南
2022/03/31 Java/Android
Python中re模块的元字符使用小结
2022/04/07 Python
css3 文字断裂效果
2022/04/22 HTML / CSS