@ZEAL Blog·厉
We stand alone,
TOGETHER.

PHP下的HTTP客户端实现

Posted by zeal on 2005-07-28 10:54 , 5291 characters |  + 0 - 0   English
转载请保留本行原始出处声明信息 : http://www.zeali.net/entry/128 MaDe1nZEAL
标签 ( PHP ):  ,  ,  ,  , 
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

Last Modified on 2007-05-25 21:18
2 条评论:
- Jack () (link) 于 2006-06-11 10:58
由于JS可以在客户端关闭,所以引入http方式检测提交的表单。这个PHP类是不是用于解决这种问题的?
请Zeal详细说说,这个类的用法。谢谢。
- zeal (link) 于 2006-06-11 14:44
相当于一个http客户端,发送http请求。
Since 2005.04.27  梦想 就像鸡蛋 要么孵化 要么臭掉RSS Feed (Entries) | Back To Home | @ZEAL | 沪ICP备05024379号