PHP在线Ping接口源码分享

聚光网络博客

发表文章数:25

首页 » 网站源码 » PHP在线Ping接口源码分享

之前在教书先生博客上看到过他分享的一个在线Ping接口的源码就测试了下发现失效了,就拿来修复下发出来吧,代码呈上。

<?php
/**
 *
 * 在线Ping
 * 聚光网络博客 https://www.rncen.com/
 *
 */
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
error_reporting(0);
function randIp()
{
    return mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255);
}

function Curl_POST($url,$post_data){
    $header=[
        'X-FORWARDED-FOR:'.randIp(),
        'CLIENT-IP:'.randIp()
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");   // 伪造ua
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
!empty($_GET['host']) ? $host=$_GET['host'] : exit(json_encode([
    "code"=>-1,
    "msg"=>"请输入网址或者IP"
],JSON_UNESCAPED_UNICODE));
$post_data = [
    "node"=>15,
    "host"=>$host
];
$array =json_decode(Curl_POST("https://www.wepcc.com/check-ping.html",$post_data));
//print($array);
//print_r($array);
function GetIP($node,$host){
    $post_data = [
        "node"=>$node,
        "host"=>$host
    ];
    $array =json_decode(Curl_POST("https://www.wepcc.com/check-ping.html",$post_data));
    if ($array->code==0){
        return [
            "code"=>-1,
            "msg"=>"解析失败,请检查输入是否正确。"
        ];
    } else {
        return [
            "time"=>$array->data->Time,
            "ttl"=>$array->data->Ttl
        ];
    }
}

if ($array->code==0){
    $data = [
        "code"=>-1,
        "msg"=>"解析失败,请检查输入是否正确。"
    ];
} else {
    $data=[
        "code"=>1,
        "msg"=>"获取成功!",
        "ip"=>$array->data->Ip,
        "ipaddress"=>$array->data->ipAddress,
        "data"=>[
            "telecom"=>[
                "time"=>$array->data->Time,
                "ttl"=>$array->data->Ttl
            ],
            "Unicom"=>GetIP(1,$host),
            "move"=>GetIP(8,$host),
            "Hong Kong"=>GetIP(7,$host),
        ]
    ];
}
exit(json_encode($data,JSON_UNESCAPED_UNICODE));

 

PHP在线Ping接口源码分享

原文地址:Ping 接口 (带 IP 归属地查询) 源码分享 - PHP - 教书先生个人博客

标签:

未经允许不得转载:作者:聚光网络博客, 转载或复制请以 超链接形式 并注明出处 聚光网络博客
原文地址:《PHP在线Ping接口源码分享》 发布于2020-07-18

分享到:
赞(1) 生成海报

评论 抢沙发

7 + 2 =


长按图片转发给朋友

Vieu4.5主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。
切换注册

登录

忘记密码 ?

切换登录

注册