php判断用户是否关注微信公众号的方法

近期遇到的问题就是,客户的需求是必须关注公众号之后才能进行相关的操作,话不多说直接上代码

     * 判断用户是否关注公众账号 function
     *
     * @Author yujiang [email protected]
     * @DateTime 2020-02-13
     * @param string $openid
     * @return boolean
     */
  function isFollow(){
        $openid=db('partner')->where('id',session('hmcuid'))->value('openid');
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxxxxxxxxxxx&secret=dxxxxxxxxxxxxxe56a118";
        $access_msg = json_decode(file_get_contents($access_token));
        $token = $access_msg->access_token;
		$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$openid."&lang=zh_CN";
		$subscribe =getJson($url);
		$zyxx = $subscribe['subscribe'];//如果是1已关注
		return $zyxx;
    }
function getJson($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}
发布了36 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41965172/article/details/104327988