微信auth授权验证类,在别人的基础上修改的,有用的上的参考下
app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect;
}
/**
* 获取授权token
*
* @param string $code 通过get_authorize_url获取到的code
*/
public function get_access_token($app_id = '', $app_secret = '', $code = '')
{
$token_url = https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code;
$token_data = $this->http($token_url);
if($token_data[0] == 200)
{
return json_decode($token_data[1], true);
}
return false;
}
/**
* 获取授权后的微信用户信息
*
* @param string $access_token
* @param string $open_id
*/
public function get_user_info($access_token = '', $open_id = '')
{
if($access_token && $open_id)
{
$info_url = https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_cn;
$info_data = $this->http($info_url);
if($info_data[0] == 200)
{
return json_decode($info_data[1], true);
}
}
return false;
}
/**
* 验证授权
*
* @param string $access_token
* @param string $open_id
*/
public function check_access_token($access_token = '', $open_id = '')
{
if($access_token && $open_id)
{
$info_url = https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$open_id}&lang=zh_cn;
$info_data = $this->http($info_url);
if($info_data[0] == 200)
{
return json_decode($info_data[1], true);
}
}
return false;
}
//curl
public function http($url, $method, $postfields = null, $headers = array(), $debug = false)
{
$ci = curl_init();
/* curl settings */
curl_setopt($ci, curlopt_http_version, curl_http_version_1_1);
curl_setopt($ci, curlopt_connecttimeout, 30);
curl_setopt($ci, curlopt_timeout, 30);
curl_setopt($ci, curlopt_returntransfer, true);
switch ($method) {
case 'post':
curl_setopt($ci, curlopt_post, true);
if (!empty($postfields)) {
curl_setopt($ci, curlopt_postfields, $postfields);
$this->postdata = $postfields;
}
break;
}
curl_setopt($ci, curlopt_url, $url);
curl_setopt($ci, curlopt_httpheader, $headers);
curl_setopt($ci, curlinfo_header_out, true);
$response = curl_exec($ci);
$http_code = curl_getinfo($ci, curlinfo_http_code);
if ($debug) {
echo =====post data======\r\n;
var_dump($postfields);
echo '=====info=====' . \r\n;
print_r(curl_getinfo($ci));
echo '=====$response=====' . \r\n;
print_r($response);
}
curl_close($ci);
return array($http_code, $response);
}
}放出输出的demo,有用的上的可以参考//微信认证链接
public function wxurl(){
import('myclass.wechatauth',app_path);
$wechat = new wechatauth();
$token = session('token'); //查看是否已经授权
if (!empty($token)) {
print_r($token);
$state = $wechat->check_access_token($token['access_token'],$token['openid']); //检验token是否可用(成功的信息:errcode:0,errmsg:ok)
print_r($state);
}
$url = $wechat->get_authorize_url('http://twx.vjiankang.org/wsite/test/wxrurl','1'); //此处链接授权后,会跳转到下方处理
echo '授权';
}
//微信返回字符串
public function wxrurl(){
import('myclass.wechatauth',app_path);
$wechat = new wechatauth();
print_r($_get); //授权成功后跳转到此页面获取的信息
$token = $wechat->get_access_token('','',$_get['code']); //确认授权后会,根据返回的code获取token
print_r($token);
session('token',$token); //保存授权信息
$user_info = $wechat->get_user_info($token['access_token'],$token['openid']); //获取用户信息
print_r($user_info);
}需要注意的是,我们用于返回的url,需要在微信公众平台-》开发者-》auth认证里修改为同一域名下
ad:真正免费,域名+虚机+企业邮箱=0元