Skip to content

Instantly share code, notes, and snippets.

@MyHoldFast
Created March 22, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MyHoldFast/b0aa2d5b974e4512897f11f49a5828ae to your computer and use it in GitHub Desktop.
Save MyHoldFast/b0aa2d5b974e4512897f11f49a5828ae to your computer and use it in GitHub Desktop.
<?php
function _curl($Url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$http_headers = array(
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding:gzip, deflate, sdch',
'Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Connection:keep-alive',
'Host:yandex.ru',
'Upgrade-Insecure-Requests:1',
'User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function getCurrentLevel($cidyId){
$timestamp = file_get_contents('https://api-maps.yandex.ru/services/coverage/1.0/layers.xml?lang=ru_UA&l=trf');
$timestamp = json_decode($timestamp)->{'data'}->{'trf'}->{'version'};
return json_decode(file_get_contents('https://jgo.maps.yandex.net/description/traffic-light?lang=ru_RU&ids='.$cidyId.'&tm='.$timestamp))->{'data'}->{'features'}[0]->{'properties'}->{'JamsMetaData'}->{'level'};
}
function getForecast($cityId){
$csrfToken = _curl('https://yandex.ru/maps/api/traffic/getForecast?csrfToken=&id='.$cityId);
$csrfToken = json_decode($csrfToken)->{'csrfToken'};
return _curl('https://yandex.ru/maps/api/traffic/getForecast?csrfToken='.$csrfToken.'&id='.$cityId);
}
echo @getCurrentLevel('213'); //возвращает текущий балл пробок
echo "<br>";
print_r(json_decode(getForecast('213'))); //возвращает json с прогнозом пробок. Если прогноза нет, то {"data":null}
// 193 = Воронеж
// 2 - Санкт-Петербург
// 213 - Москва
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment