纯PHP实现的全功能Http_Client类(支持php4,php5)
- 格式:docx
- 大小:32.49 KB
- 文档页数:35
纯PHP实现的全功能Http_Client类(支持php4,php5)
纯PHP实现的全功能 Http_Client 类(支持php4,php5)
其实用PHP模拟抓取HTTP内容,非常简单,实现方式也非常多,但本代码仍具备以下特色:
1. 纯PHP代码实现,不依赖任何其它第三方库或扩展,兼容PHP4.1起的所有版本。
2. 能设置或读取所有的HTTP头
3. 包含全功能并且带有一定智能的COOKIE处理,同一实例多次请求中会智能发送必要的COOKIE,COOKIE数据与外部文件交换(导入、导出)。
4. 可以自动处理 HTTP 301,302的跳转
5. 最可贵的一点是,支持 Keep-Alive 的HTTP连接,特别适合一次运行需求多次请求同一主机的内容情况,如采集。
6. 通过内置的Download()方法可以续传下载文件。
7. 支持通过POST方式上传任意个文件文件,发送数组字段等。
8. 支持SSL。
1.关于 Keep-Alive:
Http_Client 在没有明确发送 Connection: close 头而且WEB服务器也支持保持连接的话,会自动对请求过的连接进行缓存,
同一脚本内的下一次针对同一主机(IP)的请求将会直接采用这条连接,而不需重新建立连接。
如:for ($i = 1; $i < 5; $i++)
$http->Get('/doc/3717772065.html,'); 这样并不会为每次调用重复建立HTTP连接。
2. 关于 Cookies:
cookie 已经成为 HTTP 交互中非常重要的一个环节,像所有跟SESSION活动有关的行为均默认会通过COOKIE来
传递SESSIONID,而很多站点也开始利用COOKIE的支持与否来判断是否为ROBOT采集。所以如果不能支持COOKIE
将会使 HTTP_CLIENT 黯然失色。
COOKIE的处理在 HTTP_Client 中是隐含行为,每次请求完成后您可以调用 $http->getCookie([string key = null]);
来取得当前的 cookie,在同一脚本周期内请求同一域名范围的URL时,将会智能的根据需求发送所有之前接收到的COOKIE,
就像缺省的浏览器行为一样,而用户无需多余的操作。
HTTP的所有COOKIE数据保存在相应的数组中,在HTTP_CLIENT结束后如果有必要,您可以调用
$http->saveCookies('/path/to/file'); 将得到的COOKIES都保存到 /path/to/file 中,等下一次开启新的
HTTP_CLIENT 实例时,可以调用
$http->loadCookies('/path/to/file'); 把之前存下的COOKIE载入。
3. 关于 Debug:
由于 Http_Client 的 head/post/get 返回值均为响应body而不包含 header,所以取得HTTP头必须另行调用
$http->getHeader([string key = null]); 若不传入 key 则返回上一请求的所有HTTP头。
如果有情况需要 debug 显示完全的交互过程,可以在创建
http_client 实例时传入 true 作为构造函数的参数,
那么将会直接输出详尽的交互信息。
/**
* Full featured Http Client class in pure PHP (4.1+)
*
* API list:
* Object $http = new Http_Client([bool $verbose = false]);
* integer $http->getStatus();
* string $http->getTitle();
* string $http->getUrl();
* void $h
ttp->setHeader(string $key[, string $value = null]);
* mixed $http->getHeader([string $key = null]);
* void $http->setCookie(string $key, string $value);
* mixed $http->getCookie([string $key = null[, string $host
= null]]);
* bool $http->saveCookie(string $filepath);
* bool $http->loadCookie(string $filepath);
* void $http->addPostField(string $key, mixed $value);
* void $http->addPostFile(string $key, string $filename[,
string $content = null]);
* string $http->Get(string $url[, bool $redirect = true]);
* mixed $http->Head(string $url[, bool $redirect = true]);
* string $http->Post(string $url[, bool redirect = true]);
* bool $http->Download(string $url[, string $filepath = null[,
bool overwrite = false]);
*
* @author hightman
* @link /doc/3717772065.html,/
* @copyright Copyright © 2008-2010 Twomice Studio * @version $Id: http_client.class.php,v 1.22 2010/10/16
16:42:47 hightman Exp $
*/
/**
* Defines the package name.
*/
define ('HC_PACKAGENAME', 'HttpClient');
/**
* Defines the package version.
*/
define ('HC_VERSION', '2.0-beta');
/**
* This constant defines how many times should be tried on
I/O failure (timeout and error).
* Defaults to 3, it should be greater than 0.
*/
define ('HC_MAX_RETRIES', 3);
/**
* Http_Client is a full featured client class for the HTTP
protocol.
*
* It currently implements some HTTP/1.x protocols, including
request method HEAD, GET, POST,
* and automatic handling of authorization, redirection
request, and cookies.
*
* Features include:
* 1) Pure PHP code, none of extensions is required, PHP version just from 4.1.0;
* 2) Ability to set/get any HTTP request headers, such as user-agent, referal page, etc;
* 3) Includes full featured cookie support, automatic sent
cookie header if required on next request;
* 4) Handle redirected requests automatically (such as
HTTP/301, HTTP/302);
* 5) Support real Keep-Alive connections, used for multiple
requests;
* 6) Can resume getting a partially-downloaded file use
special download() method;
* 7) Support multiple files upload via post method, support
array named request variable (arr[]=...)
* 8) SSL support
*
* The whole library code is open and free, you can use it for
any purposes.
*
* @author hightman
* @version 2.0-beta $
*/
class Http_Client
{
/**
* local private variables
* @access private
*/
var $headers, $status, $title, $cookies, $socks, $url, $filepath,
$verbose;
var $post_files, $post_fields;