PHP整合 - 欺诈交通探测器

PHP整合

PHP中的基本集成

作为示例代码,我们将使其尽可能简单。首先是向我们的端点发出curl请求。我们将使用json_decode($ response,1);将响应传递给数组( $output );解决响应,我们将在$http_status中收到请求的状态

<?php
$ip = '148.56.53.217'; //-- IP咨询
$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);

错误响应的类型

$http_status (整数)中,我们可以收到以下值:

-400这表明参数不正确。

-429这表示我们已达到计划所允许的每天最大请求数。

$output (Array)中的错误响应示例:

Array
(
    [status] => error
    [code] => Bad params
)

为了控制请求中的错误,我们将执行以下操作:

if($http_status != 200) {
    echo $output['code']; //-- 会警告我们我们犯了什么样的错误
}

正确答案

答案正确后,我们将在$http_status (整数)中收到以下值:

-200个正确的请求。

$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($http_status == 200) {
    //-- 如果结果为1,我们将继续阻止用户
    if($output['data']['block'] == 1) {
        //-- 阻止用户的行为
    }
}