讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 7.3.2 網頁判斷用戶是否關注 >

7.3.2 網頁判斷用戶是否關注

以下PHP代碼實現了判斷用戶是否關注的功能。


 1 require_once('weixin.class.php');
 2 $weixin = new class_weixin;
 3 $openid = "";
 4 if (!isset($_GET["code"])){
 5     $redirect_url = 'http:// '.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 6     $jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_base", "123");
 7     Header("Location: $jumpurl");
 8     exit;
 9 }else{
10     $access_token = $weixin->oauth2_access_token($_GET["code"]);
11     $openid = $access_token['openid'];
12     $info = $weixin->get_user_info($openid);
13     if ($info["subscribe"] == 1){
14         $href = "http:// www.baidu.com/";
15     }else{
16         $href = "http:// mp.weixin.qq.com/s?__biz=MzA5NzM2MTI4OA==&mid=203240737&
           idx=1&sn=007bbbe06fb89cbce76d6f8b619acc1a&scene=0#wechat_redirect";
17     }
18     Header("Location: $href");
19     exit;
20 }
  

圖7-23 OAuth2.0獲取個人信息