Knowing about your website visitors or customers is always a challenging task. If you don’t have your customers details, then you can not target them properly. Due to this, the websites owners or companies are always looking for their customers details to make long term relations with them to take their business to the new heights.
If you’re a PHP programmer or running a website then you’re always looking for the website visitor details like location, language, timezone and also identify the IP address to avoid the security threat from bots, crawlers etc. You can easily get all details like this about website visitors using Ipstack API with website visitors IP Address.
The Ipstack API is powerful realtime IP to Geolocation API that locate website visitors by IP address. The API is very fast and return results within milliseconds into JSON or XML format data.
So here in this tutorial, you will learn how to Integrate or Consume IPStack API with PHP to get website user information
Also, read:
So let’s proceed to integrate IPStack API with PHP to get website user details.
Step1: Get Ipstack API Access Key
The API Access Key is necesary to access the ipstack API. So first you need to register ipstack API and API Access Key
We will make ipstack API request with IP address and API Access Key to get user details. You can also pass multiple IP addresses as comma separated to get bulk IP lookup.
https://api.ipstack.com/134.201.250.155?access_key=API_ACCESS_KEY
Step2: Make HTTP GET Request to get Ipstack API Data
After gettting API Acess Key, we will make HTTP API request with PHP Curl. We will store website visitors Ip addresses and API Access key into a variable. Then we will make HTTP API request to API URL https://api.ipstack.com/ with details to get IP address details. The response data with be return as JSON and stored into $resultData variable.
<?php
$ipAddress = '134.201.250.155';
$apiAccessKey = 'API_ACCESS_KEY';
$ch = curl_init('https://api.ipstack.com/'.$ipAddress.'?access_key='.$apiAccessKey.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultData = curl_exec($ch);
curl_close($ch);
?>
Step3: Display Website Visitor’s IPn Address Data
We will decode the response JSON data using json_decode() PHP function and display result data.
<?php
$result = json_decode($resultData, true);
echo $result['location']['capital'];
?>
Step4: Complete Code To GET User Data with Ipstack API using PHP
Here is the complete code to get Website visitors IP Address details for the IP address 134.201.250.155
<?php
$ipAddress = '134.201.250.155';
$apiAccessKey = 'API_ACCESS_KEY';
$ch = curl_init('https://api.ipstack.com/'.$ipAddress.'?access_key='.$apiAccessKey.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultData = curl_exec($ch);
curl_close($ch);
$result = json_decode($resultData, true);
echo $result['location']['capital'];
?>
Below is the response result in JSON format from ipstack API for the IP Address 134.201.250.155
{ "ip": "134.201.250.155", "hostname": "134.201.250.155", "type": "ipv4", "continent_code": "NA", "continent_name": "North America", "country_code": "US", "country_name": "United States", "region_code": "CA", "region_name": "California", "city": "Los Angeles", "zip": "90013", "latitude": 34.0453, "longitude": -118.2413, "location": { "geoname_id": 5368361, "capital": "Washington D.C.", "languages": [ { "code": "en", "name": "English", "native": "English" } ], "country_flag": "https://assets.ipstack.com/images/assets/flags_svg/us.svg", "country_flag_emoji": "🇺🇸", "country_flag_emoji_unicode": "U+1F1FA U+1F1F8", "calling_code": "1", "is_eu": false }, "time_zone": { "id": "America/Los_Angeles", "current_time": "2018-03-29T07:35:08-07:00", "gmt_offset": -25200, "code": "PDT", "is_daylight_saving": true }, "currency": { "code": "USD", "name": "US Dollar", "plural": "US dollars", "symbol": "$", "symbol_native": "$" }, "connection": { "asn": 25876, "isp": "Los Angeles Department of Water & Power" }, "security": { "is_proxy": false, "proxy_type": null, "is_crawler": false, "crawler_name": null, "crawler_type": null, "is_tor": false, "threat_level": "low", "threat_types": null } }
Step5: Conclusion
In this tutorial we have explained how to get website visitors IP address details using ipstack API with PHP. You can also gone through documentation for more details and advance options to integrate into your application. You can also integrate the API in other language as well.
You may also like:
- Get Geolocation Data with IPWHOIS.IO API
- IP Geolocation API Integration with PHP
- Geocoding using Positionstack API with PHP
- Get Real-time Flight Data using Aviationstack API with PHP
- User Agent Lookup using Userstack API with PHP
- Reverse IP Lookup using ipapi with PHP
- Weather Data with Weatherstack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Scrape SERP Data with SerpStack API using PHP
- Scraping Review Data using ReviewAPI with PHP
- Search Result Scraping with Zenserp API using PHP
- Web Scraping with Zenscrape API using PHP
- Web Scraping with Real-Time ScrapeStack REST API using PHP