Hello, newbie to ipinfo and curl. Have some code based on the ipinfo php code snippet (IP address geolocation in PHP).
Code appears to work identifying country code, however, account stats are not showing any activity.
I suspect the two lines:
$curl = curl_init($endpoint . $ip); // should it be $curl = curl_init($endpoint . $ip. "?token=".$TOKEN);
curl_setopt($curl, CURLOPT_USERPWD, "$TOKEN:");
Any help appreciated. Also any advice on better code! Full code:
// geo location check based on code from https://ipinfo.io/blog/ip-geolocation-in-php/
$ip = '';
$cc = '';
$check_cc = 'NZ';
// Set the API endpoint
$endpoint = "https://ipinfo.io/";
// Set the IP address
$ip = empty($_SERVER['REMOTE_ADDR']) ? NULL : $_SERVER['REMOTE_ADDR'];
// API token via <https://ipinfo.io/signup>
$token = "removed";
// Initialize curl session
$curl = curl_init($endpoint . $ip);
// Set curl options for token and to return content as string
curl_setopt($curl, CURLOPT_USERPWD, "$TOKEN:");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute the session, making the request
$response = curl_exec($curl);
// close cURL session, and free up system resources
curl_close($curl);
echo PHP_EOL;
// Converts JSON string into a PHP object
$json = json_decode($response, true);
// get country code details of the API by accessing object properties
if ( is_array($json) ) {
$cc = $json['country'];
}
if ($cc <> $check_cc) {
echo "country code: ".$cc. "is not NZ";
}
// end geo location check