php版微信公众平台之微信网页登陆授权示例
本文实例讲述了php版微信公众平台之微信网页登陆授权。分享给大家供大家参考,具体如下:
微信公众平台实现微信网页登陆授权开发其实是非常的简单了,因为官方的参考程序了,下面小编就看了一站长根据官方参考做的一个网页登陆授权例子,大家可看看.
文件1:index.php
//换成自己的接口信息
$appid='XXXXX';
header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=127.0.0.1/oauth.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect');
 
 
 
 
 
 
 
文件二:oauth.php,代码如下:
<?php
$code=$_GET['code'];
$state=$_GET['state'];
//换成自己的接口信息
$appid='XXXXX';
$appsecret='XXXXX';
if(emptyempty($code))$this->error('授权失败');
$token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token=json_decode(file_get_contents($token_url));
if(isset($token->errcode)){
echo'<h1>错误:</h1>'.$token->errcode;
echo'<br/><h2>错误信息:</h2>'.$token->errmsg;
exit;
}
$access_token_url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token=json_decode(file_get_contents($access_token_url));
if(isset($access_token->errcode)){
echo'<h1>错误:</h1>'.$access_token->errcode;
echo'<br/><h2>错误信息:</h2>'.$access_token->errmsg;
exit;
}
$user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';//开源软件:phpfensi.com
//转成对象
$user_info=json_decode(file_get_contents($user_info_url));
if(isset($user_info->errcode)){
echo'<h1>错误:</h1>'.$user_info->errcode;
echo'<br/><h2>错误信息:</h2>'.$user_info->errmsg;
exit;
}
//打印用户信息
echo'<pre>';
print_r($user_info);
echo'</pre>';
?>
 
 
 
 
 
 
 
 
 
 
到此网页登陆授权开发功能就作完了,如果想要获取用户基本信息我们需要看另一个例子,在官方有说明大家可自行搜索哦.
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
