$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;
}
PHP 验证被微信封域名
最新推荐文章于 2021-03-28 09:28:18 发布