子比友链检测及api接口

友链界面

图片[1]-子比友链检测及api接口-皓月星辰

路径:/wp-content/themes/zibll/pages/links.php 注意备份原文件,

在主题 pages下新建一个模板,重新命名不妨碍后续更新问题,新建界面选这个模板就可以,自行修改图片地址

子比主题根目录自行建立func.php 自己修改api地址:$url

/*友情链接检测
* 毕方资源网:www.bfbke.com
*/
function xy_link(){
    global $wpdb;
    $links = $wpdb->prefix . 'links';
    $user_url = !empty($_REQUEST['user_url']) ? $_REQUEST['user_url'] : '';
    $xypro_url = esc_url(home_url());
   
     

    $url = 'https://pay.vs98k.cn/urlApi.php?myurl=' . urlencode($xypro_url) . '&targeturl=' . urlencode($user_url);
    
    //初始化
      $response = wp_remote_get($url, array(
        'timeout' => 20,
        'sslverify' => false,
    ));
     if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        error_log("HTTP request error: " . $error_message);
        zib_send_json_error(array('code' => -1, 'msg' => '请求数据错误: ' . $error_message));
        return;
    }
     $http_code = wp_remote_retrieve_response_code($response);
    $body = wp_remote_retrieve_body($response);
    
    $deta = date("Y-m-d H:i:s", time()+8*60*60);
    
    
     if ($body) {
        $data = json_decode($body, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            error_log("JSON decode error: " . json_last_error_msg());
            zib_send_json_error(array('code' => -1, 'msg' => 'JSON解析错误: ' . json_last_error_msg()));
            return;
        }
        if ($data['code'] == 200 && isset($data['data']['message']) && $data['data']['message'] == '友情链接存在!') {
            $link_visible = 'Y';
            $msg = '检测友情链接正常!';
        } else {
            $link_visible = 'N';
            $msg = isset($data['data']['message']) ? $data['data']['message'] : '未获得URL相关数据,请重试!';
        }
    } else {
        $link_visible = 'N';
        $msg = '请求数据错误';
    }
    $link_con = array(
        'link_visible' => $link_visible,
        'link_updated' => $deta,
        'link_notes'   => $msg,
    );
    $wpdb->update($links,$link_con,array('link_url'=> $user_url));
    zib_send_json_success(array('code' => 0, 'msg' => $msg));
}
add_action('wp_ajax_xy_link', 'xy_link');

function xy_link_jc(){
    global $wpdb;
    $links = $wpdb->prefix . 'links';
    $deta = date("Y-m-d H:i:s", time()+8*60*60);
    $link_YN = !empty($_REQUEST['link_id']) ? $_REQUEST['link_id'] : '';
    $link_yn = mb_substr($link_YN, 0, 1, 'utf-8');
    $link_id = substr_replace($link_YN,"",0,1);
    
    if($link_yn == 'N'){
        $link_con = array(
            'link_visible' => 'N',
            'link_updated' => $deta,
            'link_notes'   => '请确认您已经添加本站的链接',
        );
        $wpdb->update($links,$link_con,array('link_id'=> $link_id));
        zib_send_json_success(array('code' => -1, 'msg' => '已将链接隐藏'));
    }elseif($link_yn == 'Y'){
        $link_con = array(
            'link_visible' => 'Y',
            'link_updated' => $deta,
            'link_notes'   => '检测友情链接正常!',
        );
        $wpdb->update($links,$link_con,array('link_id'=> $link_id));
        zib_send_json_success(array('code' => 0, 'msg' => '已将链接显示'));
    }elseif($link_yn == 'D'){
        $del = $wpdb->delete( $links, array( 'link_id' => $link_id ) );
        if($del){
            zib_send_json_success(array('code' => 0, 'msg' => '删除成功'));
        }
    }
}
add_action('wp_ajax_xy_link_jc', 'xy_link_jc');

function Links(){
    global $wpdb;
    $links = $wpdb->prefix . 'links';
    $links_count = $wpdb->get_var( "SELECT COUNT(`link_id`) FROM {$links}" );
    if(get_current_user_id() == 1){
        $gl = true;
        $gl_td = '<th align="center" class="xy-width-100 xy_hide">管理</th>';
    }
                    //<span class="but jb-pink inspect" style="float: right;font-size: 12px;">一键检测</span>
    $html = '
        <div class="zib-widget">
            <h2>友情链接检测

            </h2>
            <div class="xypro_describe"> 
                <p class="xy_height_hide_p2 xy_display_btn">查看更多友链 <i class="fa fa-angle-down ml6 xy-more-btn"></i></p>
                <p class="xy_height_hide_p"></p>
                <div class="xypro_describe_title">
                    检测列表('.$links_count.'条友链)
                </div> 
                <div id="xy_hide" class="xypro_describe_content xy_height_hide">
                    <table class="yq">
                        <thead>
                            <tr>
                                <th align="center" class="xy-width-190">检测时间</th>
                                <th align="center">网站名称</th>
                                <th align="center">下链原因</th>
                                '.$gl_td.'
                            </tr>
                        </thead>';
    $html .= '          <tbody class="link-list">';
    
    //获取分页数据 :每页显示的数量 默认为50
    $limit = 15;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    //计算每一页第一条记录的显示偏移量
    //偏移量 = (页码 -1) \* 每页的显示数量
    $offset = ( $paged - 1 ) * $limit;
    
    $total = $wpdb->get_var( "SELECT COUNT(`link_id`) FROM $links" );
    $pages = ceil( $total / $limit );
    
    //调用
    $link = $wpdb->get_results("SELECT * FROM $links WHERE link_id ORDER BY link_id desc LIMIT {$limit} OFFSET {$offset}");
    
    foreach ($link as $k => $v){
        $name = $v->link_name;
        $btn = '<a href="javascript:;" class="but jb-pink testing" style="float:right;font-size:12px" id="'.$v->link_url.'">检测</a>';
        $btn = get_current_user_id() == 1 ? $btn : '';
        if($v->link_notes == '检测友情链接正常!'){
            $msg_btn = 'color: green;';
        }else{
            $msg_btn = 'color: red;';
        }
        if($gl){
            $gl_tr_td = '<td class="xy_hide">
            <a  href="javascript:;" class="admin-btn">管理</a>
            <ul class="admin-guanli">
                <li><a class="link_JC" href="javascript:;" id="Y'.$v->link_id.'">显示</a></li>
                <li><a class="link_JC" href="javascript:;" id="N'.$v->link_id.'">隐藏</a></li>
                <li><a class="link_JC" href="javascript:;" id="D'.$v->link_id.'">删除</a></li>
            </ul>
            </td>';
        }
        if($v->link_notes == '检测友情链接正常!'){
            $title = '检测友情链接正常';
            $name = '<a href="'.$v->link_url.'" target="_blank" data-toggle="tooltip" data-original-title="'.$v->link_description.'">'.$name.'</a>';
        }elseif($v->link_notes == '未获得URL相关数据,请重试!'){
            $title = '请检查网站是否开了重定向';
        }elseif($v->link_notes == '请确认您已经添加本站的链接'){
            $title = '请检查该链接是否做了本站友链';
        }elseif(!$v->link_notes){
            $title = '该网站站长还未检测';
        }else{
            $title = '检测网站访问时间过长';
        }
        $link_notes = $v->link_notes ? $v->link_notes : '该网站站长还未检测';
        $html .= '<tr class="yq_link">
                    <td>'.$v->link_updated.'</td>
                    <td>'.$name.'</td>
                    <td style="'.$msg_btn.'"><span data-toggle="tooltip" data-original-title="'.$title.'">'.$link_notes.'</span><span class="xy_hide">'.$btn.'</span></td>
                    '.$gl_tr_td.'
                </tr>';
    }
    
    $html .= '<script>
 
        var clicktype = true;
        $(".link_JC").on("click",function(e){
            if(confirm(\'是否进入下一步操作?\')) {
                if(clicktype == false){return false};
                clicktype = false;
                var link_id = $(this).attr("id");
                $.ajax({
                    type:"POST",
                    url:"'.admin_url('admin-ajax.php').'",
                    data:{
                        "action":"xy_link_jc",
                        "link_id":link_id,
                    },
                    cache:false,
                    dataType:"json",
                    success:function(data){
                        if(data.code == 0){
                            notyf(data.msg,"success");
                        }else{
                            notyf(data.msg,"warning");
                        }
                        clicktype = true;
                    },
                    error:function(data){
                        notyf("请求数据错误","warning");
                        clicktype = true;
                    }
                });
            }
        });
    </script>';
    
    $html .= '<script>
        var clicktype = true;
        $(".testing").on("click",function(e){
            if(clicktype == false){return false};
            clicktype = false;
            var link_url = $(this).attr("id");
            notyf("检测中请稍等...","load", "2");
            var text = "<i class=loading mr6></i> 检测中";
            $(this).attr("disabled",true);
            $(this).text("");
            $(this).append(text);
            $(this).addClass("jc-load");
            $.ajax({
                    type:"POST",
                    url:"'.admin_url('admin-ajax.php').'",
                    data:{
                        "action":"xy_link",
                        "user_url":link_url,
                    },
                    cache:false,
                    dataType:"json",
                    success:function(data){
                        if(data.code == 0){
                            notyf(data.msg,"success");
                        }else{
                            notyf(data.msg,"warning");
                        }
                        clicktype = true;
                        $(".jc-load").text("");
                        $(".jc-load").append("检测");
                        $(".jc-load").attr("disabled",false);
                      //  link_url.removeClass("jc-load");
                    },
                    error:function(data){
                        notyf("请求数据错误","warning");
                        clicktype = true;
                        $(".jc-load").text("");
                        $(".jc-load").append("检测");
                        $(".jc-load").attr("disabled",false);
                       // link_url.removeClass("jc-load");
                    }
                });
        })
    </script>';
    
    $html .= '<script>
        $(document).on("click",".xy_display_btn",function(){
            $(".xy_display_btn").remove();
            $(".xy_height_hide_p").remove();
            $("#xy_hide").removeClass("xy_height_hide");
            $(".xypro_describe").append("<p class=\'xy_height_hide_p2 xy_hide_btn\'>隐藏友链列表 <i class=\'fa fa-angle-up ml6 xy-more-btn\'></i></p>");
        });
        $(document).on("click",".xy_hide_btn",function(){
            $(".xy_hide_btn").remove();
            $("#xy_hide").addClass("xy_height_hide");
            $(".xypro_describe").append("<p class=\'xy_height_hide_p2 xy_display_btn\'>查看更多友链 <i class=\'fa fa-angle-down ml6 xy-more-btn\'></i></p><p class=\'xy_height_hide_p\'></p>");
            $("body,html").animate({scrollTop:$(".xypro_describe").offset().top},400);

        });
    </script>';
    $html .= '</tbody></table>';
    $html .= $link ? xy_pages($pages,$paged) : '<div class="text-center ajax-item "><img style="width:280px;opacity: .7;" src="https://cos.vs98k.cn/uploads/2024/07/03/null.svg"><p style="" class="em09 muted-3-color separator">暂无友链链接</p></div>';
    $html .= '</div></div></div>';
    return $html;
}
/**
* 数字分页函数
* 因为wordpress默认仅仅提供简单分页
* 所以要实现数字分页,需要自定义函数      
*/
function xy_pages($max_page,$paged) {
    $html = '';
    $html.= '<style>
        .pagination{margin:30px 0;padding:0 10px;text-align:center;font-size:12px;display:block;border-radius:0}
        .excerpts .pagination{margin-bottom: 10px;}
        .pagination ul{display:inline-block !important;margin-left:0;margin-bottom:0;padding:0}
        .pagination ul > li{display:inline}
        .pagination ul > li > a,.pagination ul > li > span{margin:0 2px;padding:6px 12px;background-color:#ddd;color:#666;border-radius:2px;opacity:.88}
        .pagination ul > li > a:hover,.pagination ul > li > a:focus{opacity:1}
        .pagination ul > .active > a,.pagination ul > .active > span{background-color:#1d1d1d;color:#fff}
        .pagination ul > .active > a,.pagination ul > .active > span{cursor:default}
        .pagination ul > li > span,.pagination ul > .disabled > span,.pagination ul > .disabled > a,.pagination ul > .disabled > a:hover,.pagination ul > .disabled > a:focus{color:#999999;background-color:transparent;cursor:default}
    </style>';
    
    $p = 2;
    if ( $max_page == 1 ) {
        return;
    }
    $html.= '<span class="pagination"><ul>';
    $paged = !empty( $paged ) ? $paged : 1;
    
    if ( $paged > 1 ) {
        $html.= '<li><a href="'.esc_html( get_pagenum_link( 1 ) ).'">首页</a></li>';
    }
    
    $html.= '<li class="prev-page">';
    $html.= '</li>';
    for ( $i = $paged - $p; $i <= $paged + $p; $i++ ) {
        if ( $i > 0 && $i <= $max_page ) {
            if($i == $paged){
                $html.= "<li class=\"active\"><span>{$i}</span></li>";
            }else{
                $html.= '<li><a href="'.esc_html( get_pagenum_link( $i ) ).'">'. $i .'</a></li>';
            }
        }
    }
    $html.= '<li class="next-page">';
    $html.= '</li>';
    $html.= '<li><a href="'.esc_html( get_pagenum_link( $max_page ) ).'">尾页</a></li>';
    //$html.= '<li><span>共 '.$max_page.' 页</span></li>';
    $html.= '</ul></span>';
    return $html;
}

/*友情链接检测*/

api接口

<?php
header('Content-Type: application/json; charset=utf-8');
//毕方资源网:www.bfbke.com
// 检查是否传入了 myurl 和 targeturl 参数
if (!isset($_GET['myurl']) || !isset($_GET['targeturl'])) {
    echo json_encode(['code' => 400, 'data' => ['message' => '缺少参数: myurl 或 targeturl']], JSON_UNESCAPED_UNICODE);
    exit;
}

$myurl = $_GET['myurl'];
$targeturl = $_GET['targeturl'];

// 检查参数是否为空
if (empty($myurl) || empty($targeturl)) {
    echo json_encode(['code' => 400, 'data' => ['message' => '参数 myurl 或 targeturl 不能为空']], JSON_UNESCAPED_UNICODE);
    exit;
}

// 模拟友情链接存在的逻辑。你可以根据实际需求替换这个部分。
function check_link($myurl, $targeturl) {
    // 示例逻辑:简单检查目标URL页面内容中是否包含来源URL
    $html = @file_get_contents($targeturl);
    if ($html === FALSE) {
        return false;
    }
    return strpos($html, $myurl) !== false;
}

if (check_link($myurl, $targeturl)) {
    echo json_encode([
        'code' => 200,
        'data' => [
            'message' => '友情链接存在!',
            'myurl' => $myurl,
            'targeturl' => $targeturl,
            'title' => '检测成功'
        ]
    ], JSON_UNESCAPED_UNICODE);
} else {
    echo json_encode([
        'code' => 404,
        'data' => [
            'message' => '友情链接不存在!',
            'myurl' => $myurl,
            'targeturl' => $targeturl,
            'title' => '检测失败'
        ]
    ], JSON_UNESCAPED_UNICODE);
}
?>
温馨提示:

本文最后更新于2024-09-22 19:45:36,某些文章具有时效性。

若有解压密错误或链接已失效,请在下方留言或在 微信公众号留言


获取解压密码方式:

搜索并关注公众号【资源星球ResourceStar】

回复关键字【皓月星辰】即可免费获取

喜欢就支持一下吧
点赞90 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容