0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

laravel8 で 並列 post 通信

Posted at

並列処理。それは音速の処理。

・郵便番号から座標取得
・最寄り駅取得

の同時処理が可能になる。

下準備

hoge.php

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Pool;

処理

hoge.php

//        $postal = 1378088;
$postal = 4530013;

//        http://geoapi.heartrails.com/api.html#nearest heart rails api
$url = "http://geoapi.heartrails.com/api/xml?method=getStations&postal=".$postal;
$response = Http::get($url);

$responses = Http::pool(fn (Pool $pool) => [
    $pool->get('http://geoapi.heartrails.com/api/json', [
        'method' => 'searchByPostal',
        'postal' => $postal
    ]),
    $pool->get('http://geoapi.heartrails.com/api/json', [
        'method' => 'getStations',
        'postal' => $postal
    ])
]);


$res = [
    'searchByPostal' => [],
    'getStations' => [],
];

if($responses[0]->ok()){
    if(!empty($responses[0]->json()['response']['location'])){
        $res['searchByPostal'] = $responses[0]->json()['response']['location'][0];
    }
}


if($responses[1]->ok()){
    if(!empty($responses[1]->json()['response']['station'])) {
        $res['getStations'] = $responses[1]->json()['response']['station'][0];
    }
}



ざっと計測したけど、2つの処理を1つのスピードで処理できた。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?