Skip to content

Commit 2465ba7

Browse files
authored
Geoext: Add official Yandex Locator (#75)
Mobile Yandex did not worked
1 parent 0380835 commit 2465ba7

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

config.php-distr

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ define('DEFAULT_LON', 37.64);
4747
define('DEFAULT_RAD', 2);
4848

4949
define('YMAPS_APIKEY', '');
50+
define('YLOCATOR_APIKEY', '');
5051

5152
// Users
5253
define('LOGIN_MIN', 5);

geoext.php

+50
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function GetGeolocationServices()
4545
{
4646
return array(
4747
'Yandex',
48+
//'YandexLocator',
4849
'Microsoft',
4950
//'AlterGeo',
5051
//'Mylnikov',
@@ -92,6 +93,55 @@ function GetFromYandex($bssid)
9293
}
9394
return $result;
9495
}
96+
function GetFromYandexLocator($bssid)
97+
{
98+
geoDbg("yandex_locator: $bssid");
99+
$url = 'http://api.lbs.yandex.net/geolocation';
100+
$apiKey = YLOCATOR_APIKEY;
101+
if (empty($apiKey))
102+
{
103+
return '';
104+
}
105+
$xmlRequest = '<?xml version="1.0" encoding="UTF-8"?>
106+
<ya_lbs_request xmlns="http://api.lbs.yandex.net/geolocation">
107+
<common>
108+
<version>1.0</version>
109+
<api_key>'.$apiKey.'</api_key>
110+
</common>
111+
<wifi_networks>
112+
<network>
113+
<mac>'.$bssid.'</mac>
114+
</network>
115+
</wifi_networks>
116+
</ya_lbs_request>';
117+
$curl = curl_init($url);
118+
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
119+
curl_setopt($curl, CURLOPT_POST, true);
120+
curl_setopt($curl, CURLOPT_POSTFIELDS, "xml=" . urlencode($xmlRequest));
121+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
122+
$data = curl_exec($curl);
123+
124+
if (curl_errno($curl))
125+
{
126+
return '';
127+
}
128+
curl_close($curl);
129+
130+
$result = '';
131+
geoDebug('yandex_locator', $bssid, $data);
132+
$xml = simplexml_load_string($data);
133+
134+
if ($xml->position->type == 'wifi')
135+
{
136+
$latitude = $xml->position->latitude;
137+
$longitude = $xml->position->longitude;
138+
if (handleGeoErrors('yandex_locator', $bssid, $latitude, $longitude))
139+
{
140+
$result = $latitude.';'.$longitude.';yandex_locator';
141+
}
142+
}
143+
return $result;
144+
}
95145
function GetFromAlterGeo($bssid)
96146
{
97147
geoDbg("altergeo: $bssid");

index.php

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function getNews()
132132
$form = validForm(isset($_GET['fetch']) ? $_GET['fetch'] : '');
133133
if ($page == '') $page = '404';
134134

135+
// getGeoByIP($_SERVER['REMOTE_ADDR'], YLOCATOR_APIKEY)
135136
$lat = DEFAULT_LAT;
136137
$lon = DEFAULT_LON;
137138
$rad = DEFAULT_RAD;

ipext.php

+48
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,51 @@ function API_get_ranges($lat, $lon, $radius)
484484
}
485485
return $result;
486486
}
487+
488+
/**
489+
* Get geolocation based on IP Address
490+
*
491+
* @param string $ip IP Address to get location for.
492+
* @param string $apiKey Yandex Locator API key.
493+
*
494+
* @return array|null Array ("lat" => (float), "lon" => (float)) or null in case of error.
495+
*/
496+
function getGeoByIP($ip, $apiKey)
497+
{
498+
if (empty($apiKey))
499+
{
500+
return;
501+
}
502+
$url = 'http://api.lbs.yandex.net/geolocation';
503+
504+
$xmlRequest = '<?xml version="1.0" encoding="UTF-8"?>
505+
<ya_lbs_request xmlns="http://api.lbs.yandex.net/geolocation">
506+
<common>
507+
<version>1.0</version>
508+
<api_key>'.$apiKey.'</api_key>
509+
</common>
510+
<ip>
511+
<address_v4>'.$ip.'</address_v4>
512+
</ip>
513+
</ya_lbs_request>';
514+
515+
$curl = curl_init($url);
516+
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
517+
curl_setopt($curl, CURLOPT_POST, true);
518+
curl_setopt($curl, CURLOPT_POSTFIELDS, "xml=" . urlencode($xmlRequest));
519+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
520+
$data = curl_exec($curl);
521+
522+
if (curl_errno($curl))
523+
{
524+
return;
525+
}
526+
527+
curl_close($curl);
528+
529+
$xml = simplexml_load_string($data);
530+
return array(
531+
"lat" => $xml->position->latitude,
532+
"lon" => $xml->position->longitude
533+
);
534+
}

0 commit comments

Comments
 (0)