
How to get your website visitors using Userparser API in PHP

Understanding who your website visitors are, where they come from and their browsing habits is critical in providing a tailored user experience. With the Userparser API, you can now identify the country of origin of your website visitors quickly and easily. Read on to find out how you can use this powerful tool to improve your website's user experience!
Introduction to Userparser API using PHP
Userparser API is a free and paid API that enables developers to detect the country of their website visitors. It uses various techniques to achieve this, including examining the IP address of the visitor and looking up geo-location data. The Userparser API can be used with any programming language, but in this article we will focus on how to use it with PHP.
The Userparser API is very easy to use. First, you need to sign up for a free account at userparser.com. Once you have done this, you will be given a unique API key which you need to include in all requests made to the Userparser API. With this key included, all you need to do is make an HTTP GET request to the following URL:
https://api.userparser.com/1.1/detect?ip=visitor_ip_address
Replace " visitor_ip_address " with the actual IP address of the website visitor (this can be retrieved using the $_SERVER['REMOTE_ADDR'] variable in PHP). The response from the Userparser API will be a JSON object or XML containing information about the country of the visitor, such as the country code (ISO 3166-1 alpha-2), country name , country calling code, continent code (ISO 3166-1 alpha-3) and continent name .
Benefits of Using Userparser API and PHP for Country Information
There are many benefits of using the Userparser API and PHP to get your website visitors' country information. The most obvious benefit is that you can use this information to tailor your website content to better suit your target audience. For example, if you know that most of your visitors are from the Canada, you can adjust your content accordingly to appeal to that market.
Another benefit of using this information is that you can use it to track website traffic and analyze user behavior. This data can be very valuable in understanding how users interact with your site and what areas need improvement. Additionally, this information can be used for marketing purposes, such as targeted advertising or customizing email campaigns based on location.
Overall, the Userparser API using PHP offer a wealth of possibilities for those looking to get more out of their website traffic data. By taking advantage of these tools, you can better understand your users and deliver a more personalized experience that is sure to improve your bottom line.
How to integrate and Use Userparser API in PHP
Integrating and using Userparser API in PHP is a simple process.
- First, sign up for a free account at Userparser.com.
- Get your API KEY from this your dashboard page Userparser.com/dashboard/
- Start see data about your users using the below code.
Once you have done this, open this https://api.userparser.com/1.1/detect?ip=YOUR_IP&api_key=YOUR_API_KEY in your web browser and you will be able to see your country or your Virtual country information (If you're a VPN guy 😀) .
I've got this response based on my ip address and API key
{
"info": {
"status": "success",
"message": "Send your user's User-Agent too, So we can give you more detailed information, learn more here [ https://www.userparser.com/docs/user-agent-and-geoip-lookup-api-v1.1 ]",
"ua": null,
"ip": "206.150.64.0"
},
"location": {
"countryName": "Canada",
"countryCode2": "CA",
"countryCode3": "CAN",
"capitalCity": "Ottawa",
"continentName": "North America",
"continentCode": "NA",
"callingCode": "+1",
"currencyCode": "CAD"
}
}
I would appreciate you, if you can leave yours, here in the comment section.
Now lets see how we can make a request in our PHP project to get the above JSON data:
Here I will use curl function, to make request because it is better than file_get_contents function in many aspects such as timeout control, sending user agent , SSL verify and many more features.
$api_key = 'XXXXXXXXXXFSDGJU56SGBVB'; // ADD YOUR API KEY
$ip_address = '206.150.64.0'; // REPLACE WITH YOUR USERS IP
$url = "https://api.userparser.com/1.1/detect?api_key=$api_key&ip=$ip_address";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // SEND YOUR USER-AGENT TO MAKE MORE LEGIT
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); // GZIP DATA
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); // SET TRUE TO MAKE REQUEST ON SSL ONLY
curl_setopt($curl, CURLOPT_TIMEOUT, 1); // SET TIMEOUT LIMIT TO 1 SEC TO PREVENT TIMEOUT
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // TO RETURN THE TRANSFER AS A STRING OF THE RETURN VALUE OF curl_exec() INSTEAD OF OUTPUTTING IT DIRECTLY.
if(curl_errno($curl)){
echo 'Curl error: ' . curl_error($curl); // ERROR RESULT in CURL REQUEST
}else{
$json_response = curl_exec($curl); // ALL THE RESULT HERE
$geoData= json_decode($json_response);
if($geoData->info->status == "success"){ // IF THE RESULT RETURNS A success
echo "Country name : ".$geoData->location->countryName."
";
echo "country 2 letters code : ".$geoData->location->countryCode2."
";
echo "country 3 letters code : ".$geoData->location->countryCode3."
";
echo "capital city : ".$geoData->location->capitalCity."
";
echo "continent name : ".$geoData->location->continentName."
";
echo "continent code : ".$geoData->location->continentCode."
";
echo "calling code : ".$geoData->location->callingCode."
";
echo "currency code : ".$geoData->location->currencyCode;
}else{
echo "Error with the API response: ". $geoData->info->message;
}
curl_close($curl); // CLOSE CURL CONN
}
Here is the result on my page.

If you can't use curl in your project environment due to any reason, here you can use file_get_contents function:
$api_key = 'XXXXXXXXXXFSDGJU56SGBVB'; // ADD YOUR API KEY
$ip_address = '206.150.64.0'; // REPLACE WITH YOUR USERS IP
$url = "https://api.userparser.com/1.1/detect?api_key=$api_key&ip=$ip_address";
$json_response = file_get_contents($url);
$geoData= json_decode($json_response);
if($geoData->info->status == "success"){ // IF THE RESULT RETURNS A success
echo "Country name : ".$geoData->location->countryName."
";
echo "country 2 letters code : ".$geoData->location->countryCode2."
";
echo "country 3 letters code : ".$geoData->location->countryCode3."
";
echo "capital city : ".$geoData->location->capitalCity."
";
echo "continent name : ".$geoData->location->continentName."
";
echo "continent code : ".$geoData->location->continentCode."
";
echo "calling code : ".$geoData->location->callingCode."
";
echo "currency code : ".$geoData->location->currencyCode;
}else{
echo "Error with the API response " . $geoData->info->message;
}
Tips for Optimizing the API request of Userparser API using PHP
1) Save your visitor's IP address in session, then every time the user refreshes the page or navigates to another page, compare the stored ip on session with the actual ip address, and if the ip's are the same, simply stop making the same request to the Userparser server. As a result, you only make a request for a new user. By doing so, you will,
- Save a lot of API call counts.
- Save time on waiting the API to respond.
2) Use curl request as soon as possible because file_get_contents function is not not a secure method, has no control, and is relatively slower.
3) You can send your users User-Agent to parse too, But just send your needed data only to get it faster, So in my example I only sent the IP address so I have got the needed data only.
Conclusion
In this article, I've discussed how to use Userparser API using PHP to get your website visitors country information. I've gone through the steps of integrating the Userparser API with your website's code and provided some examples of how you can use the data returned by it. With this knowledge, you should now be able to create more customized experiences for your users based on their location.
Thank you for reading.
Happy coding 😊

Full stack Web developer, Founder of Mefth and Userparser.

Get Notified through your email for new Blogs.
You may like these posts
Report for Comment or Reply