easyhttp 轻量级语义化 HTTP 客户端开源项目

我要开发同款
匿名用户2020年03月20日
35阅读
开发技术PHP
所属分类服务器软件、HTTP服务器
授权协议MIT

作品详情

EasyHttp是一个轻量级、语义化、对IDE友好的HTTP客户端,支持常见的HTTP请求、异步请求和并发请求,让你可以快速地使用HTTP请求与其他Web应用进行通信。

EasyHttp并不强制依赖于cURL,如果没有安装cURL,EasyHttp会自动选择使用PHP流处理,或者你也可以提供自己的发送HTTP请求的处理方式。

安装说明环境依赖PHP>=5.5.0如果使用PHP流处理,allow_url_fopen必须在php.ini中启用。如果使用cURL处理,cURL>=7.19.4,并且编译了OpenSSL与zlib。一键安装composerrequiregouguoyin/easyhttp发起请求同步请求

常规请求

$response=Http::get('https://httpbin.org/get');$response=Http::post('https://httpbin.org/post');$response=Http::patch('https://httpbin.org/patch');$response=Http::put('https://httpbin.org/put');$response=Http::delete('https://httpbin.org/delete');$response=Http::head('https://httpbin.org/head');$response=Http::options('https://httpbin.org/options');

发送URL编码的请求

//application/json(默认)$response=Http::asJson()->post(...);//application/x-www-form-urlencoded$response=Http::asForm()->post(...);

发送Multipart请求

$response=Http::asMultipart('input_name',file_get_contents('photo1.jpg'),'photo2.jpg')->post('https://test.com/attachments');$response=Http::asMultipart('input_name',fopen('photo1.jpg','r'),'photo2.jpg')->post('https://test.com/attachments');$response=Http::attach('input_name',file_get_contents('photo1.jpg'),'photo2.jpg')->post('https://test.com/attachments');$response=Http::attach('input_name',fopen('photo1.jpg','r'),'photo2.jpg')->post('https://test.com/attachments');表单enctype属性需要设置成multipart/form-data

携带请求头的请求

$response=Http::withHeaders(['x-powered-by'=>'gouguoyin'])->post(...);

携带重定向的请求

//默认$response=Http::withRedirect(false)->post(...);$response=Http::withRedirect([    'max'=>5,    'strict'=>false,    'referer'=>true,    'protocols'=>['http','https'],    'track_redirects'=>false])->post(...);

携带认证的请求

//Basic认证$response=Http::withBasicAuth('username','password')->post(...);//Digest认证(需要被HTTP服务器支持)$response=Http::withDigestAuth('username','password')->post(...);

携带Token令牌的请求

$response=Http::withToken('token')->post(...);

携带认证文件的请求

$response=Http::withCert('/path/server.pem', 'password')->post(...);

携带SSL证书的请求

//默认$response=Http::withVerify(false)->post(...);$response=Http::withVerify('/path/to/cert.pem')->post(...);

携带COOKIE的请求

$response=Http::withCookies(array $cookies,string$domain)->post(...);

携带协议版本的请求

$response=Http::withVersion(1.0)->post(...);

携带代理的请求

$response=Http::withProxy('tcp://localhost:8125')->post(...);$response=Http::withProxy([    'http'=>'tcp://localhost:8125',//Usethisproxywith"http"    'https'=>'tcp://localhost:9124',//Usethisproxywith"https",    'no'=>['.com.cn','gouguoyin.cn']//Don'tuseaproxywiththese])->post(...);

设置超时时间(单位秒)

$response=Http::timeout(60)->post(...);

设置延迟时间(单位秒)

$response=Http::delay(60)->post(...);

设置并发次数

$response=Http::concurrency(10)->post(...);异步请求useGouguoyin\EasyHttp\Response;useGouguoyin\EasyHttp\RequestException;Http::getAsync('https://easyhttp.gouguoyin.cn/api/sleep3.json',[],function(Response$response){echo'请求成功,返回内容:'.$response->body().PHP_EOL;},function(RequestException$e){echo'请求异常,错误码:'.$e->getCode().',错误信息:'.$e->getMessage().PHP_EOL;});Http::postAsync(...);Http::patchAsync(...);Http::putAsync(...);Http::deleteAsync(...);Http::headAsync(...);Http::optionsAsync(...);并发请求useGouguoyin\EasyHttp\Response;useGouguoyin\EasyHttp\RequestException;$stime=microtime(true);$promises=[Http::getAsync('https://easyhttp.gouguoyin.cn/api/sleep3.json'),Http::getAsync('https://easyhttp.gouguoyin.cn/api/sleep1.json'),Http::getAsync('https://easyhttp.gouguoyin.cn/api/sleep2.json'),];Http::concurrency(10)->promise($promises,function(Response$response,$index){echo"发起第$index个请求,请求时长:".$response->json()->second.'秒'.PHP_EOL;},function(RequestException$e){echo'请求异常,错误码:'.$e->getCode().',错误信息:'.$e->getMessage().PHP_EOL;});$etime=microtime(true);$total=floor($etime-$stime);echo"当前页面执行总时长:{$total}秒".PHP_EOL;//输出发起第1个请求,请求时长:1秒发起第2个请求,请求时长:2秒发起第0个请求,请求时长:3秒当前页面执行总时长:3秒如果未调用concurrency()方法,并发次数默认为$promises的元素个数使用响应发起请求后会返回一个GouguoyinEasyHttpResponse的实例,该实例提供了以下方法来检查请求的响应:

$response->body():string;$response->json():object;$response->array():array;$response->status():int;$response->ok():bool;$response->successful():bool;$response->serverError():bool;$response->clientError():bool;$response->headers():array;$response->header($header):string;异常处理请求在发生客户端或服务端错误时会抛出GouguoyinEasyHttpRequestException异常,你可以在请求实例上调用throw方法:

$response=Http::post(...);//在客户端或服务端错误发生时抛出异常$response->throw();return$response['user']['id'];GouguoyinEasyHttpRequestException$e提供了以下方法来返回异常信息:

$response->getCode():int;$e->getMessage():string;$e->getFile():string;$e->getLine():int;$e->getTrace():array;$e->getTraceAsString():string;
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论