並列処理。それは音速の処理。
・郵便番号から座標取得
・最寄り駅取得
の同時処理が可能になる。
下準備
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つのスピードで処理できた。