Stas
8/3/2018 - 2:51 PM

Пример получения города по IP через API DaData на PHP

Пример получения города по IP через API DaData на PHP

<?php
class Dadata
{
    public function detect_by_ip($ip)
    {
        $result = false;

        if ($ch = curl_init("http://suggestions.dadata.ru/suggestions/api/4_1/rs/detectAddressByIp?ip=".$ip))
        {
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                 'Content-Type: application/json',
                 'Accept: application/json',
                 'Authorization: Token ВАШ_API_КЛЮЧ'
              ));

             $result = curl_exec($ch);
             $result = json_decode($result, true);

             curl_close($ch);
        }

        return $result;
    }
}

$result = Dadata::detect_by_ip("77.243.99.134");
print_r($result);
?>