michelle
michelle

Reputation: 653

php json query to resolve db-ip.com geolocations and store them in variables

I'm trying to use db-ip.com to resolve IP geolocations. I obtained an API key and the valid request URL as follows:

http://api.db-ip.com/addrinfo?addr=123.457.89.10&api_key=123456789

the JSON response: {"address":"11.11.11.11","country":"US","stateprov":"Texas","city":"Houston"}

This is the php script I'm executing via debian linux shell (auto-location.php)

<?php

$remoteIp = '11.11.11.11';
$geoLocationUrl="http://api.db-ip.com/addrinfo?addr=$remoteIp&api_key=123456789";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $geoLocationUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$auth = curl_exec($curl);
if($auth)
{
$json = json_decode($auth,true);
echo $address =  $json['address'];
echo $country =$json['country'];
echo $state =$json['stateprov'];
echo $city =$json['city'];
}
else{
echo 'ERROR';
}

?>

The error message below is when I execute via shell with the command-line php auto-location.php. This is barebones as possible I just want the query to work. Any help would be appreciated.

PHP Notice:  Undefined index: address in /home/auto/auto-location.php on line 15
PHP Notice:  Undefined index: country in /home/auto/auto-location.php on line 16
PHP Notice:  Undefined index: stateprov in /home/auto/auto-location.php on line 17
PHP Notice:  Undefined index: city in /home/auto/auto-location.php on line 18

Upvotes: 0

Views: 266

Answers (1)

michelle
michelle

Reputation: 653

As Luke pointed out in my comments I accidentally left the ' in my API key.

Upvotes: 1

Related Questions