HttpClient

协程Http客户端,基于\Swoole\Http\Client实现,在协程内快速发起http请求。

安装

composer require easyswoole/http-client

请求

需要在协程环境内发起请求。

请求实例

  1. $client = new \EasySwoole\HttpClient\HttpClient('http://easyswoole.com');

设置Url

可在实例化的时候,传入Url,或者如下:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setUrl('http://easyswoole.com');

设置query

通过url传入.

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setUrl('http://easyswoole.com?a=1');

通过方法传入.

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setQuery(['a' => 1]);

注意:

setQuery方法将你原本url的参数也带过来.

设置Ssl

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setEnableSSL(true);

设置等待超时时间

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setTimeout(5);

设置连接超时时间

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setConnectTimeout(10);

设置Header

设置单项:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setHeader('test','test');

设置多项:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setHeaders(['test' => 'test']);

参数:

  • $isMerge 默认:truefalse清空原有Header重新赋值。
  • $strtolower 默认:truefalse不进行小写转换。

设置Cookie

设置单项:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->addCookie('test','test');

设置多项:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->addCookies(['test' => 'test']);

参数:

  • $isMerge 默认:truefalse清空原有Cookie重新赋值。

设置XMLHttpRequest

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setXMLHttpRequest();

设置ContentType

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setContentType($client::CONTENT_TYPE_APPLICATION_XML);

json

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setContentTypeJson();

xml

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setContentTypeXml();

from-data

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setContentTypeFormData();

from-urlencode

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setContentTypeFormUrlencoded();

设置BasicAuth

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setBasicAuth('admin','admin');

设置KeepAlive

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setKeepAlive(true);

设置客户端配置

单个设置:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setClientSetting('bind_address','127.0.0.1');

批量设置:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setClientSettings([
  3. 'bind_address'=>'127.0.0.1',
  4. 'bind_port'=>'8090'
  5. ]);

设置FollowLocation

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->enableFollowLocation(5);

参数:

  • $maxRedirect 默认5,表示最多根据30x状态码进行的重定向次数。0 为关闭。

设置允许自签证书

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSslVerifyPeer(true,true);

设置服务器主机名称

与ssl_verify_peer配置配合使用

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSslHostName('');

设置验证用的Ssl证书

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSslCafile('');

设置Ssl证书目录

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSslCapath('');

设置Ssl证书文件

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSslCertFile('');

设置Ssl证书私钥文件

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSslKeyFile('');

设置代理

http代理:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setProxyHttp('127.0.0.1','1087','user','pass');

socks5代理:

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setProxySocks5('127.0.0.1','1086','user','pass');

设置端口绑定

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->setSocketBind('127.0.0.1','8090');

GET

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->get();

参数:

  • $headers 设置Header

HEAD

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->head();

DELETE

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->delete();

参数:

  • $headers 设置Header

PUT

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->put();

参数:

  • $data 请求的数据
  • $headers 设置Header

POST

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->post();

post-xml

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->postXml();

post-json

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->postJson();

参数:

  • $data 请求的数据
  • $headers 设置Header

PATCH

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->patch();

参数:

  • $data 请求的数据
  • $headers 设置Header

OPTIONS

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->options();

参数:

  • $data 请求的数据
  • $headers 设置Header

Download

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->download('./test.png');

参数:

  • $filename 保存路径
  • $offset 写入偏移量
  • $httpMethod 请求方法
  • $rawData 请求数据
  • $contentType 设置ContentType

上传文件

  1. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  2. $client->post([
  3. 'file' => new \CURLFile(__FILE__)
  4. ]);

分片上传:

  1. $file = new EasySwoole\HttpClient\Bean\CURLFile('file',__FILE__);
  2. // 设置表单的名称
  3. $file->setName('file');
  4. // 设置要文件的路径
  5. $file->setPath(__FILE__);
  6. // 设置文件总大小
  7. $file->setLength(filesize(__FILE__));
  8. // 设置offset(分片上传的关键)
  9. $file->setOffset(0);
  10. // 设置文件类型
  11. $file->setType('image/png');
  12. /** @var \EasySwoole\HttpClient\HttpClient $client **/
  13. $client->post([
  14. 'file' => $file
  15. ]);

响应

以上快速发起http请求成功后,如(GETPOST),将会返回EasySwoole\HttpClient\Bean\Response

获取响应体

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->getBody();

当响应体为json,解析:

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->json();

参数:

  • $assoc 默认falsefalseobjecttrue为数组。

当响应体为jsonp,解析:

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->jsonp();

参数:

  • $assoc 默认falsefalseobjecttrue为数组。

当响应体为xml,解析:

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->xml();

参数:

  • $assoc 默认falsefalseobjecttrue为数组。

获取错误码

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->getErrCode();

获取错误信息

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->getErrMsg();

获取响应状态码

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->getStatusCode();

获取响应头及设置的Cookie

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->getSetCookieHeaders();

获取请求的Cookie及响应头

  1. /** @var \EasySwoole\HttpClient\Bean\Response $response **/
  2. $response->getCookies();

WebSocket-Client

  1. $client = new \EasySwoole\HttpClient\HttpClient('127.0.0.1:9501');
  2. $upgradeResult = $client->upgrade(true);
  3. $frame = new \Swoole\WebSocket\Frame();
  4. //设置发送的消息帧
  5. $frame->data = json_encode(['action' => 'hello','content'=>['a'=>1]]);
  6. $pushResult = $client->push($frame);
  7. $recvFrame = $client->recv();
  8. //将返回bool或一个消息帧,可自行判断
  9. var_dump($recvFrame);

recv只会接收一次服务器的消息,如果需要一直接收,请增加while(1)死循环。