HttpClient
协程Http
客户端,基于\Swoole\Http\Client
实现,在协程内快速发起http
请求。
安装
composer require easyswoole/http-client
请求
需要在协程环境内发起请求。
请求实例
$client = new \EasySwoole\HttpClient\HttpClient('http://easyswoole.com');
设置Url
可在实例化的时候,传入Url
,或者如下:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setUrl('http://easyswoole.com');
设置query
通过url
传入.
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setUrl('http://easyswoole.com?a=1');
通过方法传入.
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setQuery(['a' => 1]);
注意:
setQuery
方法将你原本url
的参数也带过来.
设置Ssl
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setEnableSSL(true);
设置等待超时时间
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setTimeout(5);
设置连接超时时间
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setConnectTimeout(10);
设置Header
设置单项:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setHeader('test','test');
设置多项:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setHeaders(['test' => 'test']);
参数:
$isMerge
默认:true
,false
清空原有Header
重新赋值。$strtolower
默认:true
,false
不进行小写转换。
设置Cookie
设置单项:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->addCookie('test','test');
设置多项:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->addCookies(['test' => 'test']);
参数:
$isMerge
默认:true
,false
清空原有Cookie
重新赋值。
设置XMLHttpRequest
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setXMLHttpRequest();
设置ContentType
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setContentType($client::CONTENT_TYPE_APPLICATION_XML);
json
:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setContentTypeJson();
xml
:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setContentTypeXml();
from-data
:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setContentTypeFormData();
from-urlencode
:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setContentTypeFormUrlencoded();
设置BasicAuth
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setBasicAuth('admin','admin');
设置KeepAlive
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setKeepAlive(true);
设置客户端配置
单个设置:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setClientSetting('bind_address','127.0.0.1');
批量设置:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setClientSettings([
'bind_address'=>'127.0.0.1',
'bind_port'=>'8090'
]);
设置FollowLocation
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->enableFollowLocation(5);
参数:
$maxRedirect
默认5,表示最多根据30x状态码进行的重定向次数。0 为关闭。
设置允许自签证书
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSslVerifyPeer(true,true);
设置服务器主机名称
与ssl_verify_peer配置配合使用
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSslHostName('');
设置验证用的Ssl证书
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSslCafile('');
设置Ssl证书目录
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSslCapath('');
设置Ssl证书文件
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSslCertFile('');
设置Ssl证书私钥文件
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSslKeyFile('');
设置代理
http代理:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setProxyHttp('127.0.0.1','1087','user','pass');
socks5代理:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setProxySocks5('127.0.0.1','1086','user','pass');
设置端口绑定
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->setSocketBind('127.0.0.1','8090');
GET
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->get();
参数:
$headers
设置Header
HEAD
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->head();
DELETE
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->delete();
参数:
$headers
设置Header
PUT
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->put();
参数:
$data
请求的数据$headers
设置Header
POST
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->post();
post-xml
:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->postXml();
post-json
:
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->postJson();
参数:
$data
请求的数据$headers
设置Header
PATCH
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->patch();
参数:
$data
请求的数据$headers
设置Header
OPTIONS
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->options();
参数:
$data
请求的数据$headers
设置Header
Download
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->download('./test.png');
参数:
$filename
保存路径$offset
写入偏移量$httpMethod
请求方法$rawData
请求数据$contentType
设置ContentType
上传文件
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->post([
'file' => new \CURLFile(__FILE__)
]);
分片上传:
$file = new EasySwoole\HttpClient\Bean\CURLFile('file',__FILE__);
// 设置表单的名称
$file->setName('file');
// 设置要文件的路径
$file->setPath(__FILE__);
// 设置文件总大小
$file->setLength(filesize(__FILE__));
// 设置offset(分片上传的关键)
$file->setOffset(0);
// 设置文件类型
$file->setType('image/png');
/** @var \EasySwoole\HttpClient\HttpClient $client **/
$client->post([
'file' => $file
]);
响应
以上快速发起http
请求成功后,如(GET
,POST
),将会返回EasySwoole\HttpClient\Bean\Response
。
获取响应体
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->getBody();
当响应体为json
,解析:
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->json();
参数:
$assoc
默认false
,false
为object
,true
为数组。
当响应体为jsonp
,解析:
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->jsonp();
参数:
$assoc
默认false
,false
为object
,true
为数组。
当响应体为xml
,解析:
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->xml();
参数:
$assoc
默认false
,false
为object
,true
为数组。
获取错误码
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->getErrCode();
获取错误信息
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->getErrMsg();
获取响应状态码
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->getStatusCode();
获取响应头及设置的Cookie
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->getSetCookieHeaders();
获取请求的Cookie及响应头
/** @var \EasySwoole\HttpClient\Bean\Response $response **/
$response->getCookies();
WebSocket-Client
$client = new \EasySwoole\HttpClient\HttpClient('127.0.0.1:9501');
$upgradeResult = $client->upgrade(true);
$frame = new \Swoole\WebSocket\Frame();
//设置发送的消息帧
$frame->data = json_encode(['action' => 'hello','content'=>['a'=>1]]);
$pushResult = $client->push($frame);
$recvFrame = $client->recv();
//将返回bool或一个消息帧,可自行判断
var_dump($recvFrame);
recv只会接收一次服务器的消息,如果需要一直接收,请增加while(1)死循环。
当前内容版权归 EasySwoole 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 EasySwoole .