LoginSignup
5
9

More than 5 years have passed since last update.

[Guzzle]Guzzleを使って並列処理を書く

Last updated at Posted at 2016-12-15

Guzzleでの並列処理

並列処理はどうやってやるのか調べた。
本当に基本的な使い方だと思いますが、忘れないように残しておく。

Guzzle : https://github.com/guzzle/guzzle
Document : http://docs.guzzlephp.org/en/latest/

GuzzleはComposerなどでインストールしてください

処理

<?php

$client = new \GuzzleHttp\Client();

$promises[] = $client->requestAsync('GET', 'http://hogehoge');
$promises[] = $client->requestAsync('GET', 'http://hogehoge');
$promises[] = $client->requestAsync('GET', 'http://hogehoge');
$promises[] = $client->requestAsync('GET', 'http://hogehoge');
$promises[] = $client->requestAsync('GET', 'http://hogehoge');

$responses = \GuzzleHttp\Promise\all($promises)->wait();

$result = [];
foreach ($responses as $response) {
    $result[] = $response->getBody()->getContents();
}

var_dump($result);

指摘ありましたら、宜しくお願い致します。

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9