码迷,mamicode.com
首页 > 微信 > 详细

微信订阅号开发笔记(五)

时间:2014-05-08 16:45:04      阅读:726      评论:0      收藏:0      [点我收藏+]

标签:微信 php 用户管理 oauth2.0 网页授权

1、用户管理

//查询所有分组
public function queryGroups(){
$url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=";
$url.=$this->getacctoken();
$result = $this->cget($url);
header("Content-type: text/html; charset=utf-8");
print_r($result);
}
//创建分组
public function createGroup(){
$url="https://api.weixin.qq.com/cgi-bin/groups/create?access_token=";
$url.=$this->getacctoken();
$postData=‘{"group":{"name":"test"}}‘;
$result = $this->cpost($url,$postData);
header("Content-type: text/html; charset=utf-8");
print_r($result);
}
//查询用户所在分组
public function queryGroup(){
$url="https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=";
$url.=$this->getacctoken();
$postData=‘{"openid":"openId"}‘;
$result = $this->cpost($url,$postData);
header("Content-type: text/html; charset=utf-8");
print_r($result);
}
//修改分组名
public function updateGroup(){
$url="https://api.weixin.qq.com/cgi-bin/groups/update?access_token=";
$url.=$this->getacctoken();
$postData=‘{"group":{"id":100,"name":"atest"}}‘;
$result = $this->cpost($url,$postData);
header("Content-type: text/html; charset=utf-8");
print_r($result);
}
//移动用户分组
public function moveGroup(){
$url="https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=";
$url.=$this->getacctoken();
$postData=‘{
"openid": "openId",
"to_groupid": 100
}‘;
$result = $this->cpost($url,$postData);
header("Content-type: text/html; charset=utf-8");
print_r($result);
}
//获取用户基本信息
public function queryUserInfo(){
$url = "https://api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN&access_token=";
$url.=$this->getacctoken();
$url.="&openid="."openId";
$result = $this->cget($url);
header("Content-type: text/html; charset=utf-8");
echo json_encode($result);
}
//获取关注者列表
public function queryUserList(){
//https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=";
$url.=$this->getacctoken();
$result = $this->cget($url);
$r = json_encode($result);
$next_openid=$r[‘next_openid‘];
if(!empty($next_openid)){
}
header("Content-type: text/html; charset=utf-8");
echo $r ;
}

2、网页授权获取用户基本信息

 

具体而言,网页授权流程分为四步:

  1. 引导用户进入授权页面同意授权,获取code

  2. 通过code换取网页授权access_token(与基础支持中的access_token不同)

  3. 如果需要,开发者可以刷新网页授权access_token,避免过期

  4. 通过网页授权access_token和openid获取用户基本信息

 

public function index(){
//1、引导用户进入授权页面
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=";
$redirect_uri="YOU URL";
$redirect_uri=urlencode($redirect_uri);
$url.=$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=a#wechat_redirect";
$this->assign ( ‘userurl‘, $url );
$this->display();
}
public function success(){
//2、用户授权成功 获取code ,用code换取access_token
$code = I ( ‘get.code‘ );
//echo $code;
$geturl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=";
$geturl.=$code."&grant_type=authorization_code";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$geturl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output = curl_exec($ch);
curl_close($ch);
//echo json_decode($output,true);
$returnObj = json_decode($output,true);
//echo $returnObj[‘access_token‘];
//3、刷新access_token(如果需要)
//4、拉取用户信息(需scope为 snsapi_userinfo)
$geturl ="";
$geturl=" https://api.weixin.qq.com/sns/userinfo?access_token=";
$geturl.=$returnObj[‘access_token‘]."&openid=".$returnObj[‘openid‘]."&lang=zh_CN";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$geturl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
$output = curl_exec($ch);
curl_close($ch);
//echo json_decode($output,true);
$userObj = json_decode($output,true);
$this->assign ( ‘userObj‘, $userObj );
$this->display();
}

 

 

本文出自 “在路上” 博客,请务必保留此出处http://3409736.blog.51cto.com/3399736/1408372

微信订阅号开发笔记(五),布布扣,bubuko.com

微信订阅号开发笔记(五)

标签:微信 php 用户管理 oauth2.0 网页授权

原文地址:http://3409736.blog.51cto.com/3399736/1408372

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!