Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- - Get Location (ip-api)
- - Sends a request to ip-api via cURL to get the approximate location of the specified IP
- $format - What the response should look like:
- 0 => Town/City, County/State, Country
- 1 => Town/City
- 2 => County/State
- 3 => Town/City, County/State
- 4 => Country
- 5 => State Code (ie NY)
- 6 => Town/City, County/State, State Code, Country
- 7 => Array of values
- => [0] = success or failure
- => [1] = Country
- => [2] = Country Code ie. GB
- => [3] = State Code ie. NY
- => [4] = Country/County/State
- => [5] = Town/City
- => [6] = Post/ZIP Code
- => [7] = Longitude
- => [8] = Latitude
- => [9] = Timezone ie. Europe/London
- => [10] = ISP
- => [11] = ISP
- => [12] = ISP Full Name
- => [13] = The IP address
- $ip - The IP to get the location of
- $timeout - Number of seconds to wait before giving up
- */
- public function getLocation($format = 0, $ip = '', $timeout = 5) {
- if ($ip == '')
- $ip = $_SERVER['REMOTE_ADDR'];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/csv/" . $ip);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $data = curl_exec($ch);
- curl_close($ch);
- $arr = explode(",", $data);
- if ($arr[0] == "success") {
- // remove speech marks
- $i = 0;
- foreach ($arr as $ari) {
- $arr[$i] = str_replace('"', '', $ari);
- $i++;
- }
- if ($format == 0) {
- return $arr[5] . ', ' . $arr[4] . ', ' . $arr[1];
- } elseif ($format == 1) {
- return $arr[5];
- } elseif ($format == 2) {
- return $arr[4];
- } elseif ($format == 3) {
- return $arr[5] . ', ' . $arr[4];
- } elseif ($format == 4) {
- return $arr[1];
- } elseif ($format == 5) {
- return $arr[3];
- } elseif ($format == 6) {
- return $arr[5] . ', ' . $arr[4] . ', ' . $arr[3] . ', ' . $arr[1];
- } else {
- return $arr;
- }
- } else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement