-
-
Save theStrangeAdventurer/3ac75b8696512008ae3b44e25450cb1f to your computer and use it in GitHub Desktop.
get subway stations and cities of Russia superjob API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CitySubway | |
{ | |
protected static $apiCitiesUrl = "https://api.superjob.ru/2.0/towns/"; | |
protected static $apiMetroListUrl = "https://api.superjob.ru/2.0/suggest/town/:id_town/metro/all/"; | |
public static function getAllCities() | |
{ | |
$params = [ | |
'all' => 1, | |
'genitive' => 1 | |
]; | |
return self::getRequest(self::$apiCitiesUrl, $params); | |
} | |
public static function getCity($keyword) | |
{ | |
$params = [ | |
'keyword' => $keyword, | |
'genitive' => 1 | |
]; | |
return self::getRequest(self::$apiCitiesUrl, $params); | |
} | |
public static function getSubways(string $cityName) | |
{ | |
$cityId = self::getCityID($cityName); | |
$url = preg_replace('~\:id_town~', $cityId, self::$apiMetroListUrl); | |
return self::getRequest($url); | |
} | |
protected static function getRequest(string $url, array $params = []) | |
{ | |
$queryString = ''; | |
foreach ($params as $name => $value) { | |
if (empty($queryString)) $queryString .= '?'; | |
$queryString .= "&$name=$value"; | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url . $queryString); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($output); | |
} | |
public static function getCityID($keyword) | |
{ | |
$cityAr = array_shift(self::getCity($keyword)->objects); | |
return $cityAr->id ?? NULL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment