tp5 微信授权


protected $appid = '****************'; //微信 appid
protected $appsecrt = '******************'; //微信 appsecrt


//-----------静默授权 (不能获取用户的昵称、头像,要获取用户的昵称和头像使用 用户确认授权)
public function getBaseInfo()
{
//获取code
$redirect_uri = urlencode("http://www.******.com/index/index/getWxCode");//回调地址
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; //state 可任意
    $this->redirect($url, 302);// tp5的重定向方法 
}

public function getWxCode()
{
//获取access_token
//获取openID
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=
" . $this->appid . "&secret=" . $this->appsecrt . "&code=$code&grant_type=authorization_code";
    $r = http_curl($url);
$openid = $r['openid'];
$access_token = $r['access_token'];
var_dump('access_token是:' . $access_token . '=========' . 'openid是:' . $openid);
}


//-------------------------------end-----------------------------------


//-----------用户确认授权          
public function getCodeUserInfo($temp)
{
//获取code
$redirect_uri = urlencode("http://zs.zs13ce.gx.cn/index/index/getWxUserInfo");//回调地址
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=$temp&connect_redirect=1#wechat_redirect"; //state 可任意
    $this->redirect($url, 302);// tp5的重定向方法 
}


public function getWxUserInfo()
{
//换取网页授权access_token
//获取openid
$code = $this->request->param('code');
$state = $this->request->param('state');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecrt . "&code=" . $code . "&grant_type=authorization_code";
$rjson = http_curl($url);
$access_token = $rjson['access_token'];//得到access_token ( 网页的)
$openId = $rjson['openid'];//得到openid
$userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=" . $this->appid . "&lang=zh_CN";//获取用户信息
        $result = http_curl($userInfoUrl);
// var_dump($result);//打印用户详细信息
// die;
if (empty($result)) {
echo 0;die;
}
$user = Db::name("user")->where("openid", $result["openid"])->find();
if (empty($user)) {
//如果表里没有,则插入
}
$this->_user = $user;
$this->setIsSq($user);
$this->redirect(url($state), 302);// tp5的重定向方法 
}




function http_curl($url, $data=[])
{
$curl = curl_init();//初始化
curl_setopt($curl, CURLOPT_URL, $url);//设置抓取的url
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求 不验证证书和hosts
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HEADER, 0);//  设置头文件的信息作为数据流输出  设置为1 的时候,会把HTTP信息打印出来  不要http header 加快效率
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出。 如果设置是0,打印信息就是true
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data,true));
$data = curl_exec($curl);//执行命令
$result = json_decode($data, true);
if ($data == false) {
echo "Curl Error:" . curl_error($curl);
exit();
}
curl_close($curl);//关闭URL请求
return $result;
}

猜你喜欢

转载自www.cnblogs.com/j-jian/p/11908997.html