Last active
December 22, 2017 10:12
-
-
Save srsad/f4611196ca10f7c8024987f6a0a8f630 to your computer and use it in GitHub Desktop.
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 | |
// определение ip | |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { | |
$ip=$_SERVER['HTTP_CLIENT_IP']; | |
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; | |
}else{ | |
$ip=$_SERVER['REMOTE_ADDR']; | |
} | |
// https://dadata.ru/ | |
$api = '05ede21fd19ece0776055555c39201qwqe4ead36'; | |
$key = 'f1983881d43d55555529155df23428ccff8abfae'; | |
// создание cURL | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/detectAddressByIp?ip='.$ip); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
$headers = array(); | |
$headers[] = 'Accept: application/json'; | |
$headers[] = 'Authorization: Token '.$api; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = json_decode(curl_exec($ch), true); | |
$city = $result['location']['data']['city']; // эт наш город =) | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment