byancode / requestcurl
此包最新版本(dev-main)没有提供许可信息。
多Curl 异步
dev-main
2021-05-28 00:28 UTC
This package is auto-updated.
Last update: 2024-09-07 20:04:16 UTC
README
安装
您可以通过composer安装此包
composer require byancode/requestcurl
使用方法
/*************************************************************************************** @param string $method = OPTIONS, GET, POST, PUT, PATCH, DELETE, HEAD, LINK, UNLINK @param string $url = https://... @param array $fields = [ "name" => "byancode" ] or null @param array $options = [ CURLOPT_HTTPHEADER => [ "Accept" => "application/json" ] ] ***************************************************************************************/ $request->add(string $method, string $url, array $fields = null, array $options = []) $request->get(string $url, array $fields = null, array $options = []) // PROMESA $request->get(...)->then(string|array|object $response, array $request_info) $request->get(...)->catch(string|array|object $response, array $request_info, string|null $errorMessage) $request->get(...)->finally(string|array|object $response, array $request_info)
使用方法 2
# return string response $request->get(...)->then(function($data) { if (gettype($data) === 'string') { echo 'si'; } else { echo 'no'; } })->catch(function() { echo 'produced by error curl or http response error code'; }); # ---------- # output: si # return response converted in array $request->get(...)->then(function(array $data) { if (gettype($data) === 'array') { echo 'si'; } else { echo 'no'; } })->catch(function(array $responseError) { # get error response converted in array (ideal for apis) echo 'produced by error curl or http response error code'; }); # ---------- # output: si # return response converted in object $request->get(...)->then(function(object $data) { if (gettype($data) === 'object') { echo 'si'; } else { echo 'no'; } })->catch(function(array ) { echo 'produced by error curl or http response error code'; })->catch(function(): string { return 'replace response for this'; }); # ---------- # output: si
示例 1
$request = new \Byancode\RequestCurl(); for ($i=0; $i < 16; $i++) { $request->post('https://php.ac.cn/manual/es/function.symlink.php')->then(function($response) { echo '1' . PHP_EOL; echo str_repeat('-', 20) . PHP_EOL; echo $response; }); $request->add('GET', 'https://dockerd.com.cn/get-started')->then(function($response) { echo '2' . PHP_EOL; echo str_repeat('-', 20) . PHP_EOL; echo $response; }); $request->get('https://packagist.org.cn/packages/pbmedia/laravel-ffmpeg')->then(function($response) { echo '3' . PHP_EOL; echo str_repeat('-', 20) . PHP_EOL; echo $response; }); } $request->execute();
示例 2
$request = new \Byancode\RequestCurl(); $request->add('GET', 'https://restcountries.eu/rest/v2/currency/cop')->then(function(array $response): string { return $response[0]['name']; }); $request->get('https://restcountries.eu/rest/v2/currency/pen')->then(function(array $response): string { return $response[0]['name']; }); $request->get('https://restcountries.eu/rest/v2/currency/mxn')->then(function(array $response): string { return $response[0]['name']; }); $request->execute(); print_r($request->response); /* output: -------------------- Array ( [0] => Colombia [1] => Peru [2] => Mexico ) */