EasyHttp是一个轻量级、语义化、IDE友好的HTTP客户端,支持普通HTTP请求和异步请求以及并发请求,让你快速沟通与其他使用HTTP请求的Web应用程序。EasyHttp不必依赖cURL。如果没有安装cURL,EasyHttp会自动选择使用PHP流处理,或者你也可以提供自己的发送HTTP请求的处理方式。安装说明环境依赖PHP>=5.5.0如果使用PHP流处理,必须在php.ini中开启allow_url_fopen。如果使用cURL处理,cURL>=7.19.4,编译OpenSSL和zlib。一键安装composerrequiregouguoyin/easyhttp发起请求同步请求定时请求$response=Http::get('http://httpbin.org/get');$response=Http::post('http://httpbin.org/get');org/post');$response=Http::patch('http://httpbin.org/patch');$response=Http::put('http://httpbin.org/put');$response=Http::delete('http://httpbin.org/delete');$response=Http::head('http://httpbin.org/head');$response=Http::options('http//httpbin.org/options');发送Content-Type编码请求//application/json(默认)$response=Http::asJson()->post(...);//application/x-www-form-urlencoded$response=Http::asForm()->post(...);发送多部分表单请求$response=Http::asMultipart('input_name',file_get_contents('photo1.jpg'),'photo2.jpg')->post('http://test.com/attachments');$response=Http::asMultipart('input_name',fopen('photo1.jpg','r'),'photo2.jpg')->post('http://test.com/attachments');$response=Http::attach('input_name',file_get_contents('photo1.jpg'),'photo2.jpg')->post('http://test.com/附件');$response=Http::attach('input_name',fopen('photo1.jpg','r'),'photo2.jpg')->post('http://test.com/attachments');需要设置表单的enctype属性为multipart/form-data携带请求头$response=Http::withHeaders(['x-powered-by'=>'gouguoyin'])->post(...);重定向请求//Default$response=Http::withRedirect(false)->post(...);$response=Http::withRedirect(['max'=>5,'strict'=>false,'referer'=>true,'protocols'=>['http','https'],'track_redirects'=>假])->发布(...);requestwithauthentication//BasicAuthentication$response=Http::withBasicAuth('username','password')->post(...);//Digest认证(需要HTTP服务器支持)$response=Http::withDigestAuth('用户名','密码')->post(...);使用User-Agent$response=Http::withUA('Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/60.0.3100.0Safari/537.36')->post(...);携带Token令牌的请求$response=Http::withToken('to肯')->发布(...);带有身份验证文件的请求$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',//将此代理与“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)->promise(...);异步请求使用Gouguoyin\EasyHttp\Response;使用Gouguoyin\EasyHttp\RequestException;http::getAsync('http://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(...);并发请求使用Gouguoyin\EasyHttp\Response;使用Gouguoyin\EasyHttp\RequestException;$stime=微时间(真);$promises=[Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep3.json'),Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep1.json'),Http::getAsync('http://easyhttp.gouguoyin.cn/api/sleep2.json'),];Http::concurrency(10)->promise($promises,function(Response$response,$index){echo"发起$index请求,请求时长:".$response->json()->second.'second'.PHP_EOL;},function(RequestException$e){echo'请求异常,错误码:'.$e->getCode()。',错误信息:'。$e->getMessage()。PHP_EOL;});$etime=微时间(真);$total=floor($etime-$stime);echo"当前页面的总执行时间:{$total}秒".PHP_EOL;//输出发起第一个请求,请求持续时间:1秒发起第二个请求,请求持续时间:2秒发起第0次请求,请求持续时间:3秒执行当前页面持续时间:3秒如果并发()方法没有被调用,并发数默认为$promises中元素的个数。使用response发起请求会返回一个GouguoyinEasyHttpResponse的实例$response,它提供了如下方法来检查请求的response:$response->body():string;$response->json():object;$响应->数组():数组;$响应->状态():整数;$响应->确定():布尔;$响应->成功():布尔;$响应->服务器错误():布尔;$response->clientError():bool;$response->headers():array;$response->header($header):string;异常处理当客户端或服务端发生错误时,请求会抛出GouguoyinEasyHttpRequestException异常,可以在请求实例上调用throw方法:$response=Http::post(...);//客户端或服务端错误$response->throw();return$response['user']['id'];GouguoyinEasyHttpRequestException$e提供了以下方法返回异常信息:$e->getCode():int;$e->getMessage():字符串;$e->getFile():字符串g;$e->getLine():int;$e->getTrace():数组;$e->getTraceAsString():字符串;
