Created
July 1, 2016 09:11
-
-
Save MEGApixel23/633b226f5dd77f9298ca78ea80172e36 to your computer and use it in GitHub Desktop.
Simple class for GEO locating by IP. Uses http://geoip.elib.ru service
This file contains hidden or 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 | |
| class GeoIp | |
| { | |
| private $basicUrl = 'http://geoip.elib.ru/cgi-bin/getdata_mini.pl'; | |
| public function __construct(array $options = null) | |
| { | |
| if (!$options) | |
| return; | |
| if (isset($options['url'])) | |
| $this->basicUrl = $options['url']; | |
| } | |
| public function get($ip) | |
| { | |
| return $this->request(['ip' => $ip]); | |
| } | |
| private function request(array $data) | |
| { | |
| $getParams = http_build_query($data); | |
| $url = $this->basicUrl . '?' . $getParams; | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $res = curl_exec($ch); | |
| curl_close($ch); | |
| $array = $this->toArray($res); | |
| return $array; | |
| } | |
| private function toArray($res) | |
| { | |
| $array = explode("\n", $res); | |
| if (count($array) < 3) | |
| return false; | |
| $newArray = [ | |
| 'city' => $array[2], | |
| 'latitude' => $array[0], | |
| 'longitude' => $array[1], | |
| ]; | |
| return $newArray; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment