cvweiss / guzzler
一个允许在每次调用序列之间执行代码的多个调用序列的实用程序。
v0.0.9
2018-09-18 13:25 UTC
Requires
- guzzlehttp/guzzle: ^6.2
README
Guzzler
Guzzler 利用 Guzzle 的许多强大工具,并提供了一个简单的包装,用于在 PHP 中执行异步请求。Guzzle 使用 curl 作为所有请求的后端。
Guzzler 基础知识
启动
use cvweiss\Guzzler; $guzzler = new Guzzler();
执行调用
$params = []; // Parameters to share with the success and fail function. Not required, defaults to [] $headers = []; // Headers to pass, such as Content-Type or If-None-Match. Not required, defaults to [] $requestType = "GET"; // The http request type. Not required, defaults to "GET" $guzzler->call("https://example.org", "success", "fail", $params, $headers, $requestType);
要执行调用,你需要两个函数:一个用于成功请求,另一个用于失败请求。如上例所示,你必须将函数名称作为字符串传递。
function success(&$guzzler, &$params, $content) { // Do stuff with the successful return } function fail(&$guzzler, &$params, $exception) { // Do stuff with the failed return }
允许 guzzler 迭代并处理现有请求
$guzzler->tick();
完成所有 guzzler 调用
$guzzler->finish();
Guzzler 高级功能
您可以修改并发请求的数量以及 Guzzler 在调用之间等待的最大时间。等待时间考虑了调用返回并被处理所需的时间。
$guzzler = new Guzzler($concurrentRequests, $utimeSleep);
Guzzler 允许您一次性定义用户代理。默认为 "cvweiss/guzzler"
$userAgent= "MyAppName Example"; $guzzler = new Guzzler($concurrentRequests, $utimeSleep, $userAgent);
Guzzler 还允许您在最初定义 curl 选项。默认为 [CURLOPT_FRESH_CONNECT => false]
$curlOptions = [CURLOPT_FRESH_CONNECT => true]; $guzzler = new Guzzler($concurrentRequests, $utimeSleep, $userAgent, $curlOptions);
Guzzler 还允许您在每次调用时定义 curl 选项,只需在头部中嵌入带有 curl 键即可。
$headers['User-Agent'] = "MyAppName Example"; $headers['curl'] = [CURLOPT_FRESH_CONNECT => true]; $guzzler->call("https://example.org", "success", "fail", $params, $headers);
Guzzler 还简化了传递参数到在成功或失败时调用的函数的功能。
$params = ['data' = ['id' = 123, 'foo' = 'bar']]; $guzzler->call("https://example.org", "success", "fail", $params); function success(&$guzzler, &$params, $content) { $data = $params['data']; // Do stuff with $content }
Guzzler 还允许您在函数内进行额外的 guzzler 调用。
$guzzler->call("https://example.org", "success", "fail"); function success(&$guzzler, &$params, $content) { // Do stuff with $content $guzzler->call("https://example.org/example.html", "success", "fail"); }
Guzzler 使得包含正文的 POST 操作变得容易
$body = "[123, 456, 789]"; $guzzler->call("https://example.org", "success", "fail", $params, $headers, "POST", $body);
Guzzler 使得从响应中检索头部信息变得非常容易。请注意,所有头部的键都是小写。
function success(&$guzzler, &$params, $content) { $headers = $guzzler->getLastHeaders(); // Retrieves headers for this request $etag = $headers['etag'][0]; // Each header is an array (thanks curl). Use [0] to get the value of the header in most cases. }
Guzzler 使得向请求添加头部变得容易
$headers = []; $headers['User-Agent'] = "My Application"; $headers['If-None-Match'] = '"09f8b2541e00231360e70eb9d4d6e6504a298f9c8336277c704509a8"'; // ETag example $guzzler->call("https://example.org", "success", "fail", $params, $headers);
Guzzler 将验证指定的要调用的函数实际上是否存在,所以如果你输入有误,在请求实际发出之前你会立即收到通知。
$guzzler->call("https://example.org", "scucess", "fail"); // IllegalArgumentException returned since you have not defined the function 'scucess' function success(&$guzzler, &$params, $content) { }
Guzzler 使得实现 etags 变得容易。传递 $redis,它可以是任何东西,只要它正确实现了 get 和 setex。
$headers = []; $headers['etag'] = $redis; $guzzler->call("https://example.org", "success", "fail", $params, $headers); function success(&$guzzler, &$params, $content) { // $content will equal "" on a successful etag return }
Guzzler 示例
现在您已经完全阅读了手册,以下是一个完整的示例
<?php use cvweiss\Guzzler; $data = [1, 2, 3, 4, 5, 6, 7, 8, 9]; $guzzler = new Guzzler(1, 1000000); // One concurrent request, waiting up to one second on ticks foreach ($data as $id) { $guzzler->call("https://example.org?page=" . $page", "success", "fail", $params, $headers, "GET"); // Preps the request $guzzler->tick(); // Allows the above request to be started and allows previous requests to be processed and completed. } $guzzler->finish(); // Waits for all requests to be processed and completed. // This function is called when the request is successful function success(&$guzzler, &$params, $content) { echo $content . "\n"; } // This function is called when the request has failed function success(&$guzzler, &$params, $exception) { echo $exception->getMessage() . "\n"; }