Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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);

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away