微信登陆代码

浏览:694 发布日期:2017/08/10 分类:系统代码
我自己改的,但是不知道填写那里错了,麻烦高手帮我修正一下
<?php
use think\Model;
use think\Request;
class weixin extends Model{
//回调地址
public $return_url;//授权之后跳转地址
public $app_id;//公众号appID
public $app_secret;//公众号app_secret
public $app_code;//公众号获取的code参数
public function __construct($config){
// if($_COOKIE['is_mobile'] == 1)
// $this->return_url = "http://".$_SERVER['HTTP_HOST']."/index.php?m=Mobile&c=LoginApi&a=callback&oauth=qq";
// else
// $this->return_url = "http://".$_SERVER['HTTP_HOST']."/index.php?m=Home&c=LoginApi&a=callback&oauth=qq";

$this->return_url = "http://".$_SERVER['HTTP_HOST']."/index.php/Home/LoginApi/callback/oauth/weixin";

$this->app_id = $config['app_id'];
$this->app_secret = $config['app_secret'];
$this->app_code = $config['app_code'];
}
//构造要请求的参数数组,无需改动
public function login(){
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
//拼接URL
$dialog_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
. $this->app_id . "&secret=" . $this->app_secret . "&code=" . $this->code ."&grant_type=authorization_code"
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
exit;
}
/**
* weixin返回:
通过code获取access_token的接口。https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
续期access_token使用 https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
检验授权凭证(access_token)https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
获取用户个人信息 https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
appid应用唯一标识,在微信开放平台提交应用审核通过后获得 secret应用密钥AppSecret,在微信开放平台提交应用审核通过后获得 code填写第一步获取的code参数
grant_type填authorization_code
*/
public function respon(){
if($_REQUEST['state'] == $_SESSION['state'])
{
$code = $_REQUEST["code"];
//拼接URL
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?grant_type=authorization_code&"
. "client_id=" . $this->app_id . "&redirect_uri=" . urlencode($this->return_url)
. "&client_secret=" . $this->app_secret . "&code=" . $code;

$response = $this->get_contents($token_url);
if (strpos($response, "callback") !== false)
{
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
$msg = json_decode($response);
if (isset($msg->error))
{
echo "<h3>error:</h3>" . $msg->error;
echo "<h3>msg :</h3>" . $msg->error_description;
exit;
}
}
//Step3:使用Access Token来获取用户的OpenID
$params = array();
parse_str($response, $params);

$graph_url = "https://api.weixin.qq.com/sns/oauth2/access_token="
.$params['access_token'];

$str = $this->get_contents($graph_url);
if (strpos($str, "callback") !== false)
{
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($str);
if (isset($user->error))
{
echo "<h3>error:</h3>" . $user->error;
echo "<h3>msg :</h3>" . $user->error_description;
exit;
}
//获取到openid
$openid = $user->openid;
// Step5:使用Access Token以及OpenID来访问和修改用户数据
$user_info_url = "https://api.weixin.qq.com/sns/oauth2/access_token={$params['access_token']}&oauth_consumer_key={$this->app_id}&openid=$openid";
$res = $this->get_contents($user_info_url);
$res = json_decode($res,true);
$_SESSION['state'] = null; // 验证SESSION
return array(
'openid'=>$openid,// wx openid
'oauth'=>'weixin',
'nickname'=>$res['nickname'],
'head_pic'=>$res['figureurl_qq_2'],
);
}else{
return false;
}
}


public function get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);

//-------请求为空
if(empty($response)){
exit("50001");
}

return $response;
}

}


?>
评论( 相关
后面还有条评论,点击查看>>