Thank you for visiting the documentation page for the ipapi library, which provides an easy-to-use interface for interacting with the IP Geolocation API. This documentation will guide you through the process of integrating the library into your projects and making the most of its functionality.
Installation
To get started with ipapi, follow the steps below:
- Install the library via Composer by running the following command in your terminal:
composer require ipapi/ipapi
- After successful installation, include the autoloader in your PHP code:
require_once 'vendor/autoload.php';
Usage
Using the ipapi library is straightforward. Follow the example below to get the geolocation details for an IP address:
// Instantiate the IPAPI class
$ipapi = new IPAPI\IPAPI('YOUR_API_KEY');
// Request geolocation data for an IP address
$data = $ipapi->query('8.8.8.8');
// Access the geolocation data
echo 'Country: ' . $data->country_name;
echo 'City: ' . $data->city;
Authentication
To make use of the IP Geolocation API, you need an API key. If you don’t have one, visit the signup page to create a free account and generate your API key.
Once you have the API key, substitute 'YOUR_API_KEY'
in the usage example with your actual API key.
Available Methods
The ipapi library provides the following methods:
- query($ipAddress): Retrieves the geolocation data for the specified IP address.
- bulk($ipAddresses): Retrieves geolocation data for multiple IP addresses in bulk.
- outputFormat($format): Sets the output format for the response. Available formats: JSON, XML, CSV, YAML.
Error Handling
If an error occurs during the API request, an exception of type IPAPI\Exception\RequestException will be thrown. You can catch and handle this exception to display an appropriate error message to the user.
Example:
try {
$data = $ipapi->query('8.8.8.8');
} catch (IPAPI\Exception\RequestException $e) {
echo 'Error: ' . $e->getMessage();
}
Conclusion
Congratulations! You have successfully integrated the ipapi library into your project and learned how to make geolocation queries. Feel free to explore additional features and methods provided by the library, such as bulk querying and changing the output format.
If you have any further questions or need assistance, please don’t hesitate to reach out to our support team. Happy coding!