1.先获取公众号的access_token
private $appid='';
private $secret='';
private function getAccessToken() { // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 $data = S('access_token'); if ($data['expire_time'] < time()) { // 如果是企业号用以下URL获取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret; $res = json_decode($this->http_curl($url),true); $access_token = $res['access_token']; if ($access_token) { $data['expire_time'] = time() + 7000; $data['access_token'] = $access_token; S('access_token',$data); } } else { $access_token = $data['access_token']; } return $access_token; }
2.再根据"https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openid; 调用curl,根据返回的结果集里的subscribe的状态进行判断
1 public function attention(){ 2 $openid=$_SESSION['weinfo']['openid']; 3 // 获取公众号的access_token,此access_token不是用户授权后的access_token 4 $accessToken = $this->getAccessToken(); 5 $subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$openid; 6 $subscribe_info = $this->http_curl($subscribe_msg); 7 $subscribe_info = json_decode($subscribe_info); 8 if($subscribe_info->subscribe){ 9 $msg['status']=1; 10 }else{ 11 $msg['status']=0; 12 } 13 return $msg; 14 }