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、網頁授權獲取用戶基本信息
具體而言,網頁授權流程分爲四步:
引導用戶進入授權頁面同意授權,獲取code
通過code換取網頁授權access_token(與基礎支持中的access_token不同)
如果需要,開發者可以刷新網頁授權access_token,避免過期
通過網頁授權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(); }