精易论坛

标题: PHP利用curl获取站长网网站cha询IP地理位置 [打印本页]

作者: 祭祀雨人    时间: 2022-1-14 13:37
标题: PHP利用curl获取站长网网站cha询IP地理位置
本帖最后由 祭祀雨人 于 2022-1-14 13:57 编辑

昨天闲着没事,搞了个小接口
利用curl访问站长网获取IP的地理位置
代码很短,尽量备注
使用方法:
将页面放入服务器,可直接访问
直接访问将输出自己的IP和地理位置
也可以如:https://125.confly.eu.org/?ip=8.8.8.8

[PHP] 纯文本查看 复制代码
<?php
    echo '<html>
    <head>
    <title>IP地理位置接口</title></head></html>';
        //以上内容输出页面标题
if (!empty($_GET["ip"])){  //如果接口访问时带了IP参数,则cha询参数中的地址
    $local=$_GET["ip"];
} else {
    $local=$_SERVER["REMOTE_ADDR"];  //否则cha询自身本地IP地址
}
//下方代码为curl核心代码
        $url="https://ip.chinaz.com/".$local;  //访问地址
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 1);  //使用自身协yi头
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        $response = curl_exec($ch);  //返回IP138的页面源码
        $response = preg_replace('/\s+/', '', $response);  //删除源码中所有的空格
        curl_close($ch);
//curl核心代码结束,下面代码为取出中间文本
        function get_em($input, $start, $end) {
    $substr = substr($input, strlen($start)+strpos($input, $start),
    (strlen($input) - strpos($input, $end))*(-1));
    return $substr;
}
    $string = $response;
    $start = '0lh45"><em>';
    $end = '</em><aclass';
    echo 'IP:'.$local.'<br>位置:'.get_em($string, $start, $end);  //输出cha询IP和取出地理位置

?>



作者: 爱易编程    时间: 2022-6-13 08:47
写写分享,已经用上
作者: x27xjox27xjo    时间: 2022-12-6 15:32
好用哈哈哈哈  点赞点赞




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4