Skip to content

Instantly share code, notes, and snippets.

@srsad
Last active December 22, 2017 10:12
Show Gist options
  • Save srsad/f4611196ca10f7c8024987f6a0a8f630 to your computer and use it in GitHub Desktop.
Save srsad/f4611196ca10f7c8024987f6a0a8f630 to your computer and use it in GitHub Desktop.
<?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