以下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获取个人信息