Fork Copy post('https://www.iso.org/obp/ui/?v-1598682427051', [ 'headers' => [ 'Cookie' => 'JSESSIONID=3101C7434A24B4C689BB94F89DED1113; JSESSIONID=E65CD2708A42695CA884F91D8962359D; BIGipServerpool_prod_iso_www-jahia=747131274.36895.0000; BIGipServerpool_prod_iso_obp=914903434.36895.0000; BIGipServerpool_prod_iso_webstore-basket=2592625034.36895.0000; _ga=GA1.2.32995201.1598679784; _gid=GA1.2.1681507096.1598679784; _ga=GA1.1.32995201.1598679784; _gid=GA1.1.1681507096.1598679784', 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0' ], 'form_params' => [ 'v-browserDetails' => 1, 'v-loc' => 'https://www.iso.org/obp/ui/#iso:code:3166:' . $countryCode, 'v-wn' => 'obpui-105541713-0.43617479963319605', 'v-appId' => 'obpui-105541713', ] ]); $responseData = json_decode($response->getBody()->__toString(), true); $data = json_decode($responseData['uidl'], true); foreach ($data['state'] as $key => $value) { $templateContents = $data['state'][$key]['templateContents'] ?? ''; if (str_starts_with($templateContents, '{$countryCode}-([^<*]+)[^\r\n]+[\r\n].*([^<]+)"; preg_match_all("/{$regex}/", $templateContents, $matches); return array_combine($matches[1], $matches[2]); } } return null; } catch (Throwable $exception) { println($exception->getMessage()); return null; } } $data = json_decode(file_get_contents('city.json'), true); foreach ($countryCodes as $countryCode => $countryName) { try { if (isset($data[$countryCode])) { continue; } $cities = getCities($httpClient, $countryCode); $data[$countryCode] = []; if (empty($cities)) { println('Error ' . $countryCode); } else { foreach ($cities as $city => $cityName) { $data[$countryCode][] = [ 'code' => $city, 'label' => $cityName ]; } } file_put_contents('city.json', json_encode($data)); println('Saved ' . $countryCode); } catch (Throwable $exception) { println($exception->getMessage()); } }