调用示例
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>