温馨提示: 这篇文章于2742天前编写,现在可能不再适用或落后.

昨天半夜没事做,写了一个检查友链是否正常的代码。

然后gamesh的站点我打开看了一下,恩,是存在友链的,可是为啥还说没友链呢...然后查看源代码一看,凑,ajax获取的...

判断友链的原理就是去读取对方网站的源代码,然后判断里面是否有你的域名.如果用file_get_contents的话如果无法访问则会报错的,推荐还是使用curl去读取.但是读取的时候你也不知道对方的地址是否是最终显示的地址,也可能是重定向的.比如上图的最后一个neet重定向到了www下。

111111.png

function https_request($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false);//让他可以去读httpss的友链
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
function https_has_content($url,$content){
    $get = https_request($url);
    $trueUrl = $url;
    $state = "未知状态";
    if($get!==false&&$get!=null){
        //读取成功
        $header = get_headers($url);
        //检测是否重定向了
        $getContent = '';
        if(preg_match("/301/",$header[0])){
            $state = "301重定向,已转向新地址";
            //重定向,重新获取地址
            $arr = explode(" ",$header[6]);
            $path = trim(end($arr));
            $trueUrl = $path;
            $getContent = https_request($path);
            $httpsCode = 301;
        }else if(preg_match("/403/",$header[0])){
            //服务器拒绝访问
            $state = "服务器拒绝访问";
            $httpsCode = 403;
        }else if(preg_match("/404/",$header[0])){
            $state = "访问地址文件不存在";
            $httpsCode = 404;
        }else if(preg_match("/200/",$header[0])){
            //正确的路径
            $state = "访问成功";
            $getContent = $get;
            $httpsCode = 200;
        }else{
            preg_match("/[0-9]+/",$header[0],$m);
            $httpsCode = current($m);
        }

    }else{
        //无法访问
        return [
            'error'=>1,
            'state'=>'无法访问该地址',
            'oldUrl'=>$url,
            'httpsCode'=>0,
            'trueUrl'=>$trueUrl
        ];
    }
    if($getContent===false){
        //无法访问
        return [
            'error'=>1,
            'state'=>'无法访问该地址',
            'oldUrl'=>$url,
            'httpsCode'=>0,
            'trueUrl'=>$trueUrl
        ];
    }
    //preg_replace第一个参数和第二个参数都加上\给/,自动转义了....
    $content = preg_replace("///","/",$content);
    if(preg_match("/".$content."/",$getContent)){
        return [
            'error'=>0,
            'has'=>true,
            'state'=>$state,
            'oldUrl'=>$url,
            'httpsCode'=>$httpsCode,
            'trueUrl'=>$trueUrl
        ];
    }else {
        return [
            'error' => 0,
            'has' => false,
            'state'=>$state,
            'oldUrl'=>$url,
            'httpsCode'=>$httpsCode,
            'trueUrl'=>$trueUrl
        ];
    }
}

返回参数说明:

  • error:     读取是否成功 0|1

  • has:       是否存在 true|false

  • state:     字符串状态

  • oldUrl:    请求地址

  • trueUrl:   最终请求地址

  • httpsCode:  https状态码

然后调用则使用

//参数1:需要请求的地址
//参数2:你的域名
https_has_content("https://www.acgxt.com","www.acgxt.com");

PS:此方法仅用于友链非AJAX获取的网站.

    阁下需要登录后才可以查看评论哦~

    随便看看

      载入中...

      正在准备穿越次元,阁下请稍等片刻~