Zeal Blog - 泽欧里
梦想 就像鸡蛋
要么孵化 要么臭掉
WWW Zeal Blog

PHP下的HTTP客户端实现

zeal 2005-07-28 10:54 于 PHP , 5291 字 |  + 0 - 0   English
Made In Zeal 转载请保留原始链接:http://www.zeali.net/entry/128
标签 ( Tags ):  ,  ,  ,  , 
Class: Advanced HTTP Client . Author : GuinuX .

类说明:

/************************************************************************
* Class: Advanced HTTP Client
*************************************************************************
* Version       : 1.1
* Released      : 06-20-2002
* Last Modified : 06-10-2003
* Author        : GuinuX <
guinux@cosmoplazza.com>
*
*************************************************************************
* Changes
*************************************************************************
* 2003-06-10 : GuinuX
*   - Fixed a bug with multiple gets and basic auth
*   - Added support for Basic proxy Authentification
* 2003-05-25: By Michael Mauch <
michael.mauch@gmx.de>
*   - Fixed two occurences of the former "status" member
*     which is now deprecated
* 2002-09-23: GuinuX
*   - Fixed a bug to the post method with some HTTP servers
*   - Thanx to l0rd jenci <
lord_jenci@bigfoot.com> for reporting this bug.
* 2002-09-07: Dirk Fokken <
fokken@cross-consulting.com>
*   - Deleted trailing characters at the end of the file, right after the
*     php closing tag, in order to fix a bug with binary requests.
* 2002-20-06: GuinuX, Major changes
*   - Turned to a more OOP style => added class http_header,
*                                   http_response_header,
*                                   http_request_message,
*                                   http_response_message.
*       The members : status, body, response_headers, cookies,
*                     _request_headers of the http class are Deprecated.
* 2002-19-06: GuinuX, fixed some bugs in the http::_get_response() method
* 2002-18-06: By Mate Jovic <
jovic@matoma.de>
*   - Added support for Basic Authentification
*       usage: $http_client =
*                   new http( HTTP_V11, false, Array('user','pass') );
*
*************************************************************************
* Description: 
*************************************************************************
*   A HTTP client class
*   Supports :
*           - GET, HEAD and POST methods
*           - Http cookies
*           - multipart/form-data AND application/x-www-form-urlencoded
*           - Chunked Transfer-Encoding
*           - HTTP 1.0 and 1.1 protocols
*           - Keep-Alive Connections
*           - Proxy
*           - Basic WWW-Authentification and Proxy-Authentification
*
*************************************************************************
* TODO :
*************************************************************************
*           - Read trailing headers for Chunked Transfer-Encoding
*************************************************************************
* usage
*************************************************************************
* See example scripts.
*
*************************************************************************
* License
*************************************************************************
* GNU Lesser General Public License (LGPL)
*
http://www.opensource.org/licenses/lgpl-license.html
*
* For any suggestions or bug report please contact me :
*
guinux@cosmoplazza.com
************************************************************************/

这个php版的http类实现了基本和常见的http协议中的应用,包括Chunked编码的处理,Proxy和Authentification的处理。但似乎在国内站点上存在的版本都无法正常运行,原因是把源代码直接贴出来的时候经过了代码着色的处理,结果把关键的处理HTTP Response header部分的代码给篡改掉了。

原始的代码应该是:

if (preg_match("'HTTP/(\d\.\d)\s+(\d+).*'i", $tmp_headers[0], $matches )) { ... }

通过模式匹配来获取response header中的协议号和status信息。通常来说匹配出来的结果应该是 1.1 和 200 ,表示response OK.

而国内站点上的版本变成:

if (preg_match("'HTTP/(d.d)s+(d+).*'i", $tmp_headers[0], $matches )) { ... }

把用以转义的 \ 符号全部去掉了,导致的结果就是任何的http request得到的response status 都成了 -1。

完整的源代码和使用示例脚本压缩包下载(8,855 bytes): http.2003-06-10.rar http.2003-06-10.rar

This Entry was Last Modified on 2007-05-25 21:18
引用本文(TrackBack): 此功能无限期关闭ing ... 如有引用请在文中标明出处并提供超链接  
2 条评论:
- Jack () (link) 于 2006-06-11 10:58
由于JS可以在客户端关闭,所以引入http方式检测提交的表单。这个PHP类是不是用于解决这种问题的?
请Zeal详细说说,这个类的用法。谢谢。
- zeal (link) 于 2006-06-11 14:44
相当于一个http客户端,发送http请求。
对于这篇日志您有什么想法?我想听听您的高见。
如果您不是没有思想的机器Spammer,请告诉我 4 + 8 =
然后任选以下两种方式之一告诉我您的身份:
1. 使用您的 OpenID
2. 使用传统留言格式
(必填) 名字
如何称呼您?
(选填) URL
在此填写您的 Blog / Homepage 的地址,我将非常乐于访问
(选填) Email
Email地址将在经过防SPAM处理后显示在页面。如果您不希望公开自己的Email,请略过此栏
并写下您的见解:
(由于需要进行SPAM过滤,如果评论提交速度慢请耐心等待)
记住我?

  如何进行评论                   当有新回复时给我发送邮件通知
除了 <b> 和 <i> 之外,其他的HTML标签都将从您的评论中去除. URL 或 Email 地址会被自动加上链接.
当您点击发表或者预览之后,系统会自动保存您此次评论的内容,以便当网络原因发表失败时直接按Ctrl-V重新粘贴上次评论内容。
注意:快捷键及自动拷贝功能仅对IE浏览器有效。
发贴统计

Tot 622/1370

日志存档
2008年
2007年
2006年
本 Blog 原创内容
遵循以下授权:

Creative Commons
(创作共用) CC

署名-非商业性
-相同方式分享
Zeal Blog 基于
Powered byPivot - 1.24.1: 'Arcee'
Pivot1.24.1
开发
Launched @
2005-04-27
 里,居也。——《说文》    里,邑也。——《尔雅》。李注:“居之邑也。”沪ICP备05024379号 Back To Home All Links Loading