hightman / httpclient
纯PHP编写的并行HTTP客户端
v1.13
2021-10-07 23:22 UTC
Requires
- php: >=5.4.0
README
这是一个纯PHP编写的强大HTTP客户端,不需要任何其他PHP扩展。它可以帮助您轻松发送HTTP请求并处理其响应。
- 并行处理多个请求
- 完全支持HTTP方法,包括GET、POST、HEAD等
- 可定制HTTP头部,完全支持Cookie、X-Server-Ip
- 遵循301/302重定向,可以设置最大次数
- 支持Keep-Alive,重用同一主机的连接
- 支持HTTPS,使用openssl
- 允许通过POST方法上传文件
- 在DEBUG模式下提供详细信息
- 免费和开源,MIT许可证发布
要求
PHP >= 5.4.0
安装
从存档文件安装
将从httpclient-master.zip下载的存档文件解压到您的项目中。然后,将库文件添加到您的程序中
require '/path/to/httpclient.inc.php';
通过Composer安装
如果您没有Composer,您可以通过getcomposer.org上的说明进行安装。
然后,您可以使用以下命令安装此库
php composer.phar require "hightman/httpclient:*"
用法
快速使用
我们已定义了一些快捷方法,它们可以按以下方式使用
use hightman\http\Client; $http = new Client(); // 1. display response contents echo $http->get('http://www.baidu.com'); echo $http->get('http://www.baidu.com/s', ['wd' => 'php']); // 2. capture the response object, read the meta information $res = $http->get('http://www.baidu.com'); print_r($res->getHeader('content-type')); print_r($res->getCookie(null)); // 3. post request $res = $http->post('http://www.your.host/', ['field1' => 'value1', 'field2' => 'value2']); if (!$res->hasError()) { echo $res->body; // response content echo $res->status; // response status code } // 4. head request $res = $http->head('http://www.baidu.com'); print_r($res->getHeader(null)); // delete request $res = $http->delete('http://www.your.host/request/uri'); // 5. restful json requests // there are sismilar api like: postJson, putJson $data = $http->getJson('http://www.your.host/request/uri'); print_r($data); $data = $http->postJson('http://www.your.host/reqeust/uri', ['key1' => 'value1', 'key2' => 'value2']);
自定义请求
您也可以通过传递Request
对象来自定义各种请求。
use hightman\http\Client; use hightman\http\Request; $http = new Client(); $request = new Request('http://www.your.host/request/uri'); // set method $request->setMethod('POST'); // add headers $request->setHeader('user-agent', 'test robot'); // specify host ip, this will skip DNS resolver $request->setHeader('x-server-ip', '1.2.3.4'); // add post fields $request->addPostField('name', 'value'); $request->addPostFile('upload', '/path/to/file'); $request->addPostFile('upload_virtual', 'virtual.text', 'content of file ...'); // or you can specify request body directly $request->setBody('request body ...'); // you also can specify JSON data as request body // this will set content-type header to 'application/json' automatically. $request->setJsonBody(['key' => 'value']); // specify context options of connect, such as SSL options $request->contextOptions = [ 'ssl' => ['verify_peer_name' => false, 'local_cert' => '/path/to/file.pem'], ]; // execute the request $response = $http->exec($request); print_r($response);
并行多次GET请求
该库的出色功能之一是我们可以在并行中执行多个请求。例如,同时执行三个请求,总耗时是一个较长的耗时,而不是它们的总和。
use hightman\http\Client; use hightman\http\Request; use hightman\http\Response; // Define callback as function, its signature: // (callback) (Response $res, Request $req, string|integer $key); function test_cb($res, $req, $key) { echo '[' . $key . '] url: ' . $req->getUrl() . ', '; echo 'time cost: ' . $res->timeCost . ', size: ' . number_format(strlen($res->body)) . "\n"; } // or you can define callback as a class implemented interface `ParseInterface`. class testCb implements \hightman\http\ParseInterface { public function parse(Response $res, Request $req, $key) { // your code here ... } } // create client object with callback parser $http = new \hightman\http\Client('test_cb'); // or specify later as following $http->setParser(new testCb); // Fetch multiple URLs, it returns after all requests are finished. // It may be slower for the first time, because of DNS resolover. $results = $http->mget([ 'baidu' => 'http://www.baidu.com/', 'sina' => 'http://news.sina.com.cn/', 'qq' => 'http://www.qq.com/', ]); // show all results // print_r($results);
注意:还有其他方法,如:mhead、mpost、mput等。如果您需要处理多个不同的请求,可以将
Request
对象数组传递给Client::exec($reqs)
。
导出和重用Cookies
此库可以智能管理Cookies,默认存储在内存中,并在需要时发送。在Client
对象销毁后,我们可以导出所有Cookies。
$http->setCookiePath('/path/to/file');
添加Bearer授权令牌
$http->setHeader('authorization', 'Bearer ' . $token); // or add header for request object $request->setHeader('authorization', 'Bearer ' . $token);
使用代理
// use socks5 Connection::useProxy('socks5://127.0.0.1:1080'); // use socks5 with username & password Connection::useProxy('socks5://user:pass@127.0.0.1:1080'); // use HTTP proxy Connection::useProxy('http://127.0.0.1:8080'); // use HTTP proxy with basic authentication Connection::useProxy('http://user:pass@127.0.0.1:8080'); // use socks4 proxy Connection::useProxy('socks4://127.0.0.1:1080'); // disable socks Connection::useProxy(null);
启用调试模式
您可以通过Client::debug('open')
来打开调试模式。这将显示许多调试消息,以帮助您发现问题。
其他
由于Client
类也use HeaderTrait
,您可以使用Client::setHeader()
来指定由此客户端对象处理的请求的全局HTTP头部。
联系我
如果您有任何问题,请通过github issues进行报告