获取微信openID 的步骤

获取微信openid的步骤:
1、进入--》判断openID是否为空:
空-->$url=urlencode("http://xxx/xxx.php");//回调链接
$redirect="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID.
"&redirect_uri={$url}&response_type=code&scope=snsapi_base&state=survey";
echo "<script> location='".$redirect."';</script>";
以上过程会自动跳转至回调链接,同时带上两个参数:
http://xxx/xxx.php?code=0110c18ef6825s9asbbs6123a84d42fK&state=survey

跳转后:用获取到的code再换取openID

if($_GET['state']=='survey'){
$code=$_GET['code'];
$uinfo=file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".SECRET."&code={$code}&grant_type=authorization_code");
$uinfo=(array)json_decode($uinfo);
$openid=$uinfo['openid'];
$_SESSION['openid']=$openid;

}

此时判断openID是否为空:
不为空-->首先判断该openID是否存在于我们的数据库(用户表),以此来判断该用户是否已参加过活动,进而实现业务逻辑

 

posted @ 2015-07-31 11:18  明月照亮天涯  阅读(2805)  评论(0编辑  收藏  举报