PHP Code Example
A quick PHP snippet to integrate into the FraudGuard.io API and associated explanation.
This PHP code performs the following steps:
- Sets the FraudGuard.io API credentials (username and password).
- Defines the IP address to query.
- Constructs the API endpoint URL.
- Initializes a cURL session using curl_init().
- Sets various cURL options including the API URL, authentication method, credentials, and SSL verification.
- Executes the cURL request and retrieves the response.
- Checks for any errors during the request.
- Decodes the JSON response and prints it.
<?php
// FraudGuard.io API credentials
$username = 'username';
$password = 'password';
// IP address to query
$ipAddress = '8.8.8.8';
// API endpoint
$apiUrl = 'https://api.fraudguard.io/ip/' . $ipAddress;
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Enable SSL certificate verification
// Execute the cURL request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
// Decode JSON response
$responseData = json_decode($response, true);
// Print response
print_r($responseData);
}
// Close cURL session
curl_close($ch);
?>
This code demonstrates a basic integration with the FraudGuard.io API in PHP using cURL. Make sure to replace ‘username’ and ‘password’ with your actual API credentials.