Rumah > pembangunan bahagian belakang > tutorial php > 微信公众号判断用户是否已关注php代码解析_php实例

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

PHP中文网
Lepaskan: 2016-07-06 13:32:14
asal
913 orang telah melayarinya

现在的活动,很多都引导用户关注公众号,才能参与到活动当中,那如何才能判断用户关注了公众号呢? 本文就为大家提供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;
}
? >
Salin selepas log masuk

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

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan