EasyHttp是一个轻量级、语义化、对IDE友好的HTTP客户端,支持常见的HTTP请求、异步请求和并发请求,让你可以快速地使用HTTP请求与其他Web应用进行通信。
EasyHttp并不强制依赖于cURL,如果没有安装cURL,EasyHttp会自动选择使用PHP流处理,或者你也可以提供自己的发送HTTP请求的处理方式。安装说明环境依赖PHP>=5.5.0如果使用PHP流处理,allow_url_fope必须在php.ii中启用。如果使用cURL处理,cURL>=7.19.4,并且编译了OpeSSL与zlib。一键安装composerrequiregouguoyi/easyhttp发起请求同步请求常规请求
$respose=Http::get('https://httpbi.org/get');$respose=Http::post('https://httpbi.org/post');$respose=Http::patch('https://httpbi.org/patch');$respose=Http::put('https://httpbi.org/put');$respose=Http::delete('https://httpbi.org/delete');$respose=Http::head('https://httpbi.org/head');$respose=Http::optios('https://httpbi.org/optios');发送URL编码的请求
//applicatio/jso(默认)$respose=Http::asJso()->post(...);//applicatio/x-www-form-urlecoded$respose=Http::asForm()->post(...);发送Multipart请求
$respose=Http::asMultipart('iput_ame',file_get_cotets('photo1.jpg'),'photo2.jpg')->post('https://test.com/attachmets');$respose=Http::asMultipart('iput_ame',fope('photo1.jpg','r'),'photo2.jpg')->post('https://test.com/attachmets');$respose=Http::attach('iput_ame',file_get_cotets('photo1.jpg'),'photo2.jpg')->post('https://test.com/attachmets');$respose=Http::attach('iput_ame',fope('photo1.jpg','r'),'photo2.jpg')->post('https://test.com/attachmets');表单ectype属性需要设置成multipart/form-data携带请求头的请求
$respose=Http::withHeaders(['x-powered-by'=>'gouguoyi'])->post(...);携带重定向的请求
//默认$respose=Http::withRedirect(false)->post(...);$respose=Http::withRedirect([ 'max'=>5, 'strict'=>false, 'referer'=>true, 'protocols'=>['http','https'], 'track_redirects'=>false])->post(...);携带认证的请求
//Basic认证$respose=Http::withBasicAuth('userame','password')->post(...);//Digest认证(需要被HTTP服务器支持)$respose=Http::withDigestAuth('userame','password')->post(...);携带Toke令牌的请求
$respose=Http::withToke('toke')->post(...);携带认证文件的请求
$respose=Http::withCert('/path/server.pem', 'password')->post(...);携带SSL证书的请求
//默认$respose=Http::withVerify(false)->post(...);$respose=Http::withVerify('/path/to/cert.pem')->post(...);携带COOKIE的请求
$respose=Http::withCookies(array $cookies,strig$domai)->post(...);携带协议版本的请求
$respose=Http::withVersio(1.0)->post(...);携带代理的请求
$respose=Http::withProxy('tcp://localhost:8125')->post(...);$respose=Http::withProxy([ 'http'=>'tcp://localhost:8125',//Usethisproxywith"http" 'https'=>'tcp://localhost:9124',//Usethisproxywith"https", 'o'=>['.com.c','gouguoyi.c']//Do'tuseaproxywiththese])->post(...);设置超时时间(单位秒)
$respose=Http::timeout(60)->post(...);设置延迟时间(单位秒)
$respose=Http::delay(60)->post(...);设置并发次数
$respose=Http::cocurrecy(10)->post(...);异步请求useGouguoyi\EasyHttp\Respose;useGouguoyi\EasyHttp\RequestExceptio;Http::getAsyc('https://easyhttp.gouguoyi.c/api/sleep3.jso',[],fuctio(Respose$respose){echo'请求成功,返回内容:'.$respose->body().PHP_EOL;},fuctio(RequestExceptio$e){echo'请求异常,错误码:'.$e->getCode().',错误信息:'.$e->getMessage().PHP_EOL;});Http::postAsyc(...);Http::patchAsyc(...);Http::putAsyc(...);Http::deleteAsyc(...);Http::headAsyc(...);Http::optiosAsyc(...);并发请求useGouguoyi\EasyHttp\Respose;useGouguoyi\EasyHttp\RequestExceptio;$stime=microtime(true);$promises=[Http::getAsyc('https://easyhttp.gouguoyi.c/api/sleep3.jso'),Http::getAsyc('https://easyhttp.gouguoyi.c/api/sleep1.jso'),Http::getAsyc('https://easyhttp.gouguoyi.c/api/sleep2.jso'),];Http::cocurrecy(10)->promise($promises,fuctio(Respose$respose,$idex){echo"发起第$idex个请求,请求时长:".$respose->jso()->secod.'秒'.PHP_EOL;},fuctio(RequestExceptio$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秒如果未调用cocurrecy()方法,并发次数默认为$promises的元素个数使用响应发起请求后会返回一个GouguoyiEasyHttpRespose的实例,该实例提供了以下方法来检查请求的响应:$respose->body():strig;$respose->jso():object;$respose->array():array;$respose->status():it;$respose->ok():bool;$respose->successful():bool;$respose->serverError():bool;$respose->clietError():bool;$respose->headers():array;$respose->header($header):strig;异常处理请求在发生客户端或服务端错误时会抛出GouguoyiEasyHttpRequestExceptio异常,你可以在请求实例上调用throw方法:$respose=Http::post(...);//在客户端或服务端错误发生时抛出异常$respose->throw();retur$respose['user']['id'];GouguoyiEasyHttpRequestExceptio$e提供了以下方法来返回异常信息:$respose->getCode():it;$e->getMessage():strig;$e->getFile():strig;$e->getLie():it;$e->getTrace():array;$e->getTraceAsStrig():strig;









评论