
求php 微信大神求解以下网页授权的问题 200
1个回答
展开全部
很简单吧,贴一下我封装的方法,直接调用即可
class Wechat extends Controller{
protected $appid='你的微信公众号appid';
protected $appsecret = '你的微信公众号secret ';
//redirect_url授权后跳转的地址
public function accredit($redirect_url){
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
$this->redirect($url);
}
/**
* @param $code
* @return bool|string
*/
public function getAccessToken($code){
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
$res = file_get_contents($url); //获取文件内容或获取网络请求的内容
$access_token = json_decode($res,true);
return $access_token;
}
/**
* 获取用户信息
* @param unknown $openid
* @param unknown $access_token
* @return unknown
*/
public function getWeChatUserInfo($access_token,$openid){
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
$output = file_get_contents($url);
$weChatUserInfo = json_decode($output,true);
return $weChatUserInfo;
}
原文可以访问 :http://www.phpbloger.com/article/6 来看哈!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询