之前的文章是获取调用接口的一些基本参数,继续开发微信开放平台。
一、授权公众号,获取公众号的授权参数
public function tiaozhuan()
{
$info = getData('system_config', array('type' => 'pre_auth_code'), array(), 1); ////获取保存在数据库中的pre_auth_code
$pre_auth_code = $info['value'];
$back_url = "http://www.xxxxxx.com/index.php/$APPID$/callback"; //后台设置的回调地址,用户扫完二维码之后会跳转到这个连接
$url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=wxcda7bf3c4bf44cdc&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$back_url;
header("Location:".$url);
}
二、在回调地址中,获取公众号access,并获取公众号的信息
//保存公众号信息
private function saveOpenWeixin()
{
//获取公众号access
$info = getData('system_config', array('type' => 'component_access_token'), array() , 1); //获取保存在数据库中的component_access_token
$component_access_token = $info['value'];
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$component_access_token;
$auth_code = $_GET['auth_code'];
$post = array(
'component_appid' => $this->appid, //应用的appid
'authorization_code' => $auth_code,
);
$ret=send_post($url, $post);
$author = json_decode($ret, True);
//获取公众号信息
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token;
$authorAppid = $author['authorization_info']['authorizer_appid'];
$post = array(
'component_appid' => $this->appid,
'authorizer_appid' => $authorAppid,
);
$ret = send_post($url, $post);
$userInfo = json_decode($ret, True);
//公众号信息
$authorInfo = $author['authorization_info'];
}
三、使用公众号的基本信息调用接口
//获取授权方的公众号帐号基本信息
public function demo6()
{
$info = getData('system_config', array('type' => 'component_access_token'), array(), 1); //获取保存在数据库中的component_access_token
$token = $info['value'];
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$token;
$post = array(
'component_appid' => $this->appid,
'authorizer_appid' => 'xxxxxxxxxx', //公众号appid
);
$ret = send_post($url, $post);
$res = json_decode($ret, True);
}
//微信账号网页授权
public function demo8()
{
$appid = 'xxxx'; //公众号的appid
$redirect = site_url('toupiao/'.$appid.'/callback');
//$scope = 'snsapi_base';
$scope = 'snsapi_userinfo'; //获取方式
$state = 'omllz';
$component_appid = $this->appid; //应用的appid
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect.'&response_type=code&scope='.$scope.'&state='.$state.'&component_appid='.$component_appid.'#wechat_redirect';
header("Location:".$url);
}
这是我的个人网站今日Tech 喜欢科技新闻的朋友可以收藏下。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。