✅ 导航栏加载成功

接口介绍

重要提示:本接口需携带有效的api_key参数调用,默认测试密钥为 test_key_123
项目说明
接口地址http://ping.4759.cn/api/my_ping_api.php
请求方式POST(GET仅返回接口说明)
请求格式JSON(Content-Type: application/json)
返回格式JSON(格式化输出,易读)
测试密钥test_key_123(自动生成,无需手动配置)
支持检测类型域名、IPv4地址
调用限制单授权码最多20次/分钟(防止恶意刷接口)

调用说明

1. 请求参数

参数名必选类型说明示例值
api_keystringAPI密钥(默认:test_key_123)test_key_123
targetstring待检测域名/IPwww.baidu.com / 1.1.1.1
countintPing次数(1-10,默认4)4

2. 状态码说明

状态码说明解决方案
200请求成功-
400检测目标为空填写target参数
401未授权检查api_key是否有效
403授权码为空/无效填写有效的api_key(默认test_key_123)
405请求方式错误使用POST方式
429调用频率超限等待1分钟后重试
500Ping检测失败检查目标是否可Ping/服务器网络

调用示例

1. POST请求示例(Raw JSON)

请求URL:http://ping.4759.cn/api/my_ping_api.php 请求头:Content-Type: application/json 请求体: { "api_key": "test_key_123", "target": "www.baidu.com", "count": 4 }

2. 成功返回示例

{ "code": 200, "message": "检测成功", "data": { "target": "www.baidu.com", "response_ip": "110.242.68.3", "operator": "电信", "location": "北京", "latest": "25", "min": "25", "avg": "25", "max": "25", "loss": "0%", "sent": 4, "received": 4, "packet_size": 32, "used_day": 1, "remaining_day": 99 } }

3. PHP调用示例

<?php
/**
 * PHP调用Ping检测API示例
 */

// 接口配置
$apiUrl = 'http://ping.4759.cn/api/my_ping_api.php';
$apiKey = 'test_key_123'; // 你的授权码
$target = 'www.baidu.com'; // 检测目标
$count = 4; // Ping次数

// 构建请求数据
$postData = json_encode([
    'api_key' => $apiKey,
    'target' => $target,
    'count' => $count
]);

// 初始化curl
$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $apiUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($postData)
    ],
    CURLOPT_TIMEOUT => 10
]);

// 发起请求并处理结果
$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
if ($result['code'] == 200) {
    echo "检测成功:" . json_encode($result['data'], JSON_UNESCAPED_UNICODE);
} else {
    echo "检测失败:{$result['msg']}(状态码:{$result['code']})";
}
?>

4. JavaScript调用示例

<script>
/**
 * 前端调用Ping检测API示例
 */
const apiKey = 'test_key_123';
const apiUrl = 'http://ping.4759.cn/api/my_ping_api.php';
const target = 'www.baidu.com';
const count = 4;

// 构建请求数据
const postData = {
    api_key: apiKey,
    target: target,
    count: count
};

// 发起POST请求
fetch(apiUrl, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(postData)
})
.then(response => response.json())
.then(result => {
    if (result.code === 200) {
        console.log('检测结果:', result.data);
    } else {
        console.error('检测失败:', result.msg);
    }
})
.catch(error => {
    console.error('请求失败:', error);
});
</script>

在线API测试

测试结果将显示在这里...

注意事项

温馨提示:
  • 1. 接口已自动创建配置文件,无需手动创建server_config.php;
  • 2. 测试密钥为test_key_123,生产环境可修改config/server_config.php中的private_key;
  • 3. 直接在浏览器访问接口地址(GET)会返回接口说明,并非错误;
  • 4. 若Ping检测失败,检查服务器是否能访问目标地址(如百度);
  • 5. 参数编码:特殊字符(如中文域名)需经过URL编码后传递;
  • 6. 超时处理:建议设置10秒以内的超时时间,避免接口响应慢导致程序阻塞;
  • 7. 频率限制:单授权码最多20次/分钟,超出后返回429状态码;
  • 8. 异常处理:需兼容接口返回非200状态码、JSON解析失败、网络超时等场景;
  • 9. 数据准确性:Ping延迟受服务器网络环境影响,仅供参考;
  • 10. 跨域支持:接口已配置跨域允许所有域名访问,前端可直接调用。

常见问题解答

Q1:调用接口返回429怎么办?
A1:429表示调用频率超限,需等待1分钟后再调用,或联系管理员调整限制额度。
Q2:返回500错误(Ping检测失败)?
A2:检查服务器是否能正常Ping通目标地址,或目标地址是否存在ICMP限制。
Q3:前端调用出现跨域错误?
A3:接口已配置跨域允许所有域名访问,若仍报错请检查请求方式是否为POST,或浏览器控制台的具体错误信息。
Q4:为什么返回的min/max/avg值相同?
A4:Windows系统的Ping命令不返回最小/最大延迟,仅返回平均延迟,因此三个值相同,属于正常现象。
Q5:授权码test_key_123无效?
A5:默认测试密钥仅用于演示,生产环境需使用配置文件中配置的有效授权码,或联系管理员获取。
✅ 导航栏加载成功