订阅号 笔记记录开发 php,微信订阅号开发笔记(五)
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);
}
//創(chuàng)建分組
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);
}
//移動(dòng)用戶分組
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);
}
//獲取關(guān)注者列表
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、網(wǎng)頁授權(quán)獲取用戶基本信息
具體而言,網(wǎng)頁授權(quán)流程分為四步:引導(dǎo)用戶進(jìn)入授權(quán)頁面同意授權(quán),獲取code
通過code換取網(wǎng)頁授權(quán)access_token(與基礎(chǔ)支持中的access_token不同)
如果需要,開發(fā)者可以刷新網(wǎng)頁授權(quán)access_token,避免過期
通過網(wǎng)頁授權(quán)access_token和openid獲取用戶基本信息
public function index(){
//1、引導(dǎo)用戶進(jìn)入授權(quán)頁面
$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、用戶授權(quán)成功 獲取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[‘a(chǎn)ccess_token‘];
//3、刷新access_token(如果需要)
//4、拉取用戶信息(需scope為 snsapi_userinfo)
$geturl ="";
$geturl=" https://api.weixin.qq.com/sns/userinfo?access_token=";
$geturl.=$returnObj[‘a(chǎn)ccess_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
總結(jié)
以上是生活随笔為你收集整理的订阅号 笔记记录开发 php,微信订阅号开发笔记(五)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: magento php 所需模块,mag
- 下一篇: php关联图片,PHP关联链接添加方法