Home >Backend Development >PHP Tutorial >WeChat public account determines whether the user has paid attention to php code analysis_php example

WeChat public account determines whether the user has paid attention to php code analysis_php example

PHP中文网
PHP中文网Original
2016-07-06 13:32:14880browse

Many of today’s activities guide users to follow public accounts in order to participate in the activities. So how can we judge that users have followed public accounts? This article will provide you with php code to solve the problem.

Official interface description
Get basic user information (including UnionID mechanism)

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

1. As long as there is a basic access_token and user openid, you can determine whether the user follows the public account
2. The interface url used is: https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid
3. Determine whether the subscribe field returned by the interface is 1 .[1 following, 0 not following]

Note:
1. It is determined that the user login method is silent authorization, and the user is unaware. To get the user's openid;
2. To determine the user's login, you need the support of a WeChat authentication service account, a subscription account will not work;

The following is a code example

< ? 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;
}
? >

The above is the entire content of this article, I hope it will be helpful to everyone’s study

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