PHP Integration - Fraudulent traffic detector
PHP Integration
Basic integration in PHP
As an example code, we will make it as simple as possible. The first thing will be to make a curl request to our endpoint. We will pass the response to an Array ($output) with json_decode($response, 1); to address the response, and we will receive a status of the request in $http_status
<?php
$ip = '148.56.53.217'; //-- IP to consult
$apiKey = 'your_api_key';
$headers = [
'X-Key: '.$apiKey,
];
$ch = curl_init("https://www.iphunter.info:8082/v1/ip/".$ip);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = json_decode(curl_exec($ch), 1);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Types of error responses
In $http_status (integer) we can receive the following values:
- 400 This indicates that there are parameters that are not right.
- 429 It means that we have reached the maximum number of requests per day allowed by our plan.
Example of error response in $output (Array):
Array
(
[status] => error
[code] => Bad params
)
To control the error in the request we would do the following:
if($http_status != 200) {
echo $output['code']; //-- will warn us what kind of mistake we have made
}
Correct answer
When the answer is correct we will receive in $http_status (integer) the following value:
- 200 Request made correctly.
Example of correct answer in $output (Array):
Array
(
[status] => success
[data] => Array
(
[ip] => 148.56.53.217
[ip_num] => 2486711769
[country_code] => ES
[country_name] => Spain
[city] => Madrid
[isp] => Vodafone Espana S.A.U.
[domain] => vodafone.es
[block] => 0
)
)
If the request has been correct we can do the following:
if($http_status == 200) {
//-- If the result is 1, we will proceed to block the user
if($output['data']['block'] == 1) {
//-- action to block the user
}
}