PHP 验证被微信封域名

		$APPID='XXXXXXXXXXXX';
        $SECRET='XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

        $redis = Cache::store('redis')->handler();

        //获取access_token,并缓存
        $redis_key = 'wx_hint_access_token';
        $redis_valid_time = 3600;
        $redis_list = $redis->get($redis_key);

        //缓存中没有数据
        if (!$redis_list) {
            $token = null;

            if (!$token || strlen($token) < 6) {
                $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$SECRET."");
                $res = json_decode($res, true);
                if(!isset($res['access_token'])){
                    return 2;
                    exit;
                }
                $token = $res['access_token'];
            }

            //更新缓存
            $redis->set($redis_key,$token,$redis_valid_time);

        } else {
            $token = $redis_list;//缓存中的数据
        }
        $ur = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token={$token}";
        // 主域名
        if($url) {
            $short_key = 'short_m_url_id:'.$id;
            $short_url = $redis->get($short_key);
            if(!$short_url){
                $post = array();
                $post['action'] = 'long2short';
                $post['long_url'] = $url;
                $post = json_encode($post);
                $res = http_post_res($ur, $post);
                $res = json_decode($res, true);

                $short_url = isset($res['short_url']) ? $res['short_url'] : '';

                $redis->set($short_key,$short_url,$redis_valid_time);
            }

            $res = http_post_header($short_url, '');
            if (strpos($res['redirect_url'], 'weixin110') === false) {
                //没有被封
                return 0;
            } else {
                //被微信封了
                $redis->del($short_key);
                return 1;
            }
        }
  // 获取目标网址header头
function http_post_header($remote_server, $post_string) {
    if (is_array($post_string))
    {
        $post_string = http_build_query($post_string, null, '&');
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $ssl = substr($remote_server, 0, 8) == "https://" ? TRUE : FALSE;
    if ($ssl)
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }
    $data = curl_exec($ch);
    $header = curl_getinfo($ch);
    curl_close($ch);

    return $header;
//    return $data;
}


function http_post_res($remote_server, $post_string) {
    if (is_array($post_string))
    {
        $post_string = http_build_query($post_string, null, '&');
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $ssl = substr($remote_server, 0, 8) == "https://" ? TRUE : FALSE;
    if ($ssl)
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    }
    $data = curl_exec($ch);
//    $header = curl_getinfo($ch);
    curl_close($ch);

//    return $header;
    return $data;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值