Home  >  Article  >  Backend Development  >  微信公众号判断用户是否已关注php代码解析,公众php_PHP教程

微信公众号判断用户是否已关注php代码解析,公众php_PHP教程

PHP中文网
PHP中文网Original
2016-07-11 10:36:23774browse

微信公众号判断用户是否已关注php代码解析,公众php

现在的活动,很多都引导用户关注公众号,才能参与到活动当中,那如何才能判断用户关注了公众号呢? 本文就为大家提供php代码,解决问题。

官方接口说明
获取用户基本信息(包括UnionID机制) 

http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html

1、只要有基础的access_token和用户openid就可以判断用户是否关注该公众号
2、利用的接口url为:https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid
3、判断接口返回的字段subscribe是否为1.【1关注,0未关注】

注:
1、判断用户登录的方式为静默授权,用户无感知,从而得到用户的openid;
2、判断用户登录,需要微信认证服务号的支持,订阅号不行; 

下面是代码案例

< ? php

$access_token = $this - > _getAccessToken();
$subscribe_msg = &#39;https://api.weixin.qq.com/cgi-bin/user/info?access_token=&#39;.$access_token.&#39;&openid=&#39;.$_SESSION[&#39;wecha_id&#39;];
$subscribe = json_decode($this - > curlGet($subscribe_msg));
$zyxx = $subscribe - > subscribe;

if ($zyxx !== 1) {
 echo&#39;未关注!&#39;;
}
private function _getAccessToken() {
 $where = array(&#39;token&#39; = > $this - > token);
 $this - > thisWxUser = M(&#39;Wxuser&#39;) - > where($where) - > find();
 $url_get = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39;.$this - > thisWxUser[&#39;appid&#39;].&#39;&secret=&#39;.$this - > thisWxUser[&#39;appsecret&#39;];
 $json = json_decode($this - > curlGet($url_get));
 if (!$json - > errmsg) {
 } else {
  $this - > error(&#39;获取access_token发生错误:错误代码&#39;.$json - > errcode.&#39;,微信返回错误信息:&#39;.$json - > errmsg);
 }
 return $json - > access_token;
}
? >

以上就是本文的全部内容,希望对大家的学习有所帮助。


http://www.bkjia.com/PHPjc/1138987.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1138987.htmlTechArticle微信公众号判断用户是否已关注php代码解析,公众php 现在的活动,很多都引导用户关注公众号,才能参与到活动当中,那如何才能判断用户...


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn