php并行下载文件,php – Guzzle:使用Guzzle的Pool并行文件下载:batch()和`sink`选项...

您可以使用Guzzle的Pool:batch()方法并行执行http请求.它允许您使用第三个参数中的options键为请求设置默认选项.

但是如果我需要池中不同请求的不同选项呢?我想使用池执行GET请求,并将每个响应流式传输到磁盘上的不同文件.有一个接收器选项.但是如何将这个选项的不同值应用于请求?

解决方法:

Rastor’s example几乎是正确的,但如果要为Pool()构造函数提供“选项”,则会错误地实现它.

他错过了here提到的Pool options数组的关键实现.

Guzzle文档说:

When a function is yielded by the iterator, the function is provided

the “request_options” array that should be merged on top of any

existing options, and the function MUST then return a wait-able

promise.

另外,如果你查看我链接的注释下面的Pool()代码,你可以看到Guzzle的Pool调用可调用的并给它Pool的“选项”作为参数,正是这样你应该将它应用到你的请求.

正确的优先顺序是

Per-request options > Pool options > Client defaults.

如果你没有将Pool()对象的options数组应用于你的请求对象,你将会遇到严重的错误,例如你尝试制作一个新的游泳池($client,$requests(100),[‘options’=&gt [ ‘超时’=&GT 30.0]]);.如果没有我更正的代码,您的Pool-options将根本不会应用,因为您不支持正确合并池选项,因此最终会丢弃它们.

所以这里是支持Pool()选项的正确代码:

$client = new \GuzzleHttp\Client();

$requests = function ($total) use ($client) {

for ($i = 0; $i < $total; $i++) {

$url = "domain.com/picture/{$i}.jpg";

$filepath = "/tmp/{$i}.jpg";

yield function($poolOpts) use ($client, $url, $filepath) {

/** Apply options as follows:

* Client() defaults are given the lowest priority

* (they're used for any values you don't specify on

* the request or the pool). The Pool() "options"

* override the Client defaults. And the per-request

* options ($reqOpts) override everything (both the

* Pool and the Client defaults).

* In short: Per-Request > Pool Defaults > Client Defaults.

*/

$reqOpts = [

'sink' => $filepath

];

if (is_array($poolOpts) && count($poolOpts) > 0) {

$reqOpts = array_merge($poolOpts, $reqOpts); // req > pool

}

return $client->getAsync($url, $reqOpts);

};

}

};

$pool = new Pool($client, $requests(100));

但请注意,如果您知道永远不会向新的Pool()构造函数添加任何选项,则不必支持Pool()选项.在这种情况下,您可以查看the official Guzzle docs作为示例.

官方示例如下:

// Using a closure that will return a promise once the pool calls the closure.

$client = new Client();

$requests = function ($total) use ($client) {

$uri = '127.0.0.1:8126/guzzle-server/perf';

for ($i = 0; $i < $total; $i++) {

yield function() use ($client, $uri) {

return $client->getAsync($uri);

};

}

};

$pool = new Pool($client, $requests(100));

标签:guzzle,php,guzzle6

来源: https://codeday.me/bug/20191001/1839757.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 `fabpot/goutte` 和 `guzzlehttp/guzzle` 实现简单的爬虫,你可以按照以下步骤进行操作: 1. 首先,使用 Composer 安装 `fabpot/goutte` 和 `guzzlehttp/guzzle`: ``` composer require fabpot/goutte guzzlehttp/guzzle ``` 2. 创建一个 PHP 文件,导入所需的类: ```php <?php require_once __DIR__ . '/vendor/autoload.php'; use Goutte\Client; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\RequestException; ``` 3. 创建一个 `GuzzleClient` 的实例,用于向网站发送 HTTP 请求: ```php $guzzleClient = new GuzzleClient([ 'timeout' => 60, 'connect_timeout' => 60, 'headers' => [ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Accept-Encoding' => 'gzip, deflate, br', 'Accept-Language' => 'en-US,en;q=0.9', ], ]); ``` 4. 创建一个 `Goutte` 的实例,用于解析 HTML: ```php $goutteClient = new Client(); $goutteClient->setClient($guzzleClient); ``` 5. 使用 `$goutteClient` 发送 HTTP 请求,并使用 `$goutteCrawler` 解析 HTML: ```php $url = 'https://www.example.com'; $response = $goutteClient->request('GET', $url); $goutteCrawler = $goutteClient->setResponse($response); ``` 6. 现在你可以使用 `$goutteCrawler` 来查找 HTML 元素,并提取所需的信息: ```php $goutteCrawler->filter('a')->each(function ($node) { echo $node->attr('href') . "\n"; }); ``` 7. 最后,记得处理可能发生的异常: ```php try { // ... } catch (RequestException $exception) { echo $exception->getMessage(); } ``` 这就是使用 `fabpot/goutte` 和 `guzzlehttp/guzzle` 实现简单爬虫的基本步骤。当然,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值