此功能必须是服务号或者测试号,订阅号没有此权限。测试号可以在【开发】=》【开发者工具】=》【公众平台测试账号】申请,本文以测试号为例:
一:公众平台设置回调域名和测试账号
注:设置成你网站域名,不要加协议,举例:www.aaa.com,本地域名也可以
二:将下面Oauth.php放到此域名下(更改成你自己的appID和appsecret):
<?php
class Oauth
{
private $appID = 'xxxxxxx';
private $appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
private $code;
private $token;
private $openid;
//授权地址,未授权会跳转到这个路径进行授权(power方法的访问路径)
private $power_url = 'http://www.wx.com/Oauth.php?method=power';
//授权成功回调地址,授权成功后会跳转到这个路径获取用户信息(getUser方法的访问路径)
private $redirect_uri = 'http://www.wx.com/Oauth.php?method=getUser';
//授权日志目录
private $log_filedir = "./logs/oauth/";
//获取用户信息
function getUser() {
//判断用户session是否存在,存在直接返回[省略]
//没有用户session则去授权获取
$this->code = $_GET['code'];
$token = $this->getToken();
if($token['code']){
header('location:' . $this->power_url);//跳转到授权页面
}
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$this->token.'&openid='.$this->openid.'&lang=zh_CN';
$user = $this->send($url);
$this->_log('获取用户信息:' . $this->ajax($user));
if(!isset($user['openid'])) {
$this->_log('获取用户信息失败!');
return "获取信息失败,请重新进入";
}
//入库存session,将openid作为唯一标识[省略]
//返回用户信息
return $this->ajax($user);
}
//获取token
public function getToken() {
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appID.'&secret='.$this->appsecret.'&code='.$this->code.'&grant_type=authorization_code';
$token = $this->send($url);
$this->_log('获取access_token:' . $this->ajax($token));
if(isset($token['access_token'])) {
$this->token = $token['access_token'];
$this->openid = $token['openid'];
return ['code'=>0, 'msg'=>'获取access_token成功'];
}else{
return ['code'=>1, 'msg'=>'获取access_token失败'];
}
}
//网页授权
function power() {
//请求授权
header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appID.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
}
/**
* @param $url
* @param null $_input
* @param string $data_type
* @return mixed
* $_input= ["post"=>[],"refer"=>"",cookiefile='']
*/
function send($url, $input=null, $data_type='json') {
$ch = curl_init();
$useragent = isset($input['useragent']) ? $input['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_POST, isset($input['post']) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
if( isset($input['post']) ) curl_setopt( $ch, CURLOPT_POSTFIELDS, $input['post'] );
if( isset($input['refer']) ) curl_setopt( $ch, CURLOPT_REFERER, $input['refer'] );
curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($input['timeout']) ? $input['timeout'] : 5 ) );
$result = curl_exec( $ch );
curl_close( $ch );
if ($data_type == 'json') {
$result = json_decode($result,true);
}
return $result;
}
//日志记录
private function _log($log_content){
!is_dir($this->log_filedir) && mkdir($this->log_filedir, 0755, true);//目录不存在则创建目录
$log_filename = $this->log_filedir . date("Ymd") . '.log';//日志文件路径
file_put_contents($log_filename, '['.date("H:i:s").']' .PHP_EOL . $log_content . PHP_EOL."------------------------ --------------------------".PHP_EOL, FILE_APPEND);
}
function ajax($data)
{
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
}
$oauth = new Oauth();
$method = isset($_GET['method'])?$_GET['method']:'getUser';
echo $oauth->$method();
三:打开微信开发者工具,访问【域名/Oauth.php】
<?php class Oauth{private $appID = 'xxxxxxx';private $appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
private $code;private $token;private $openid;
//授权地址,未授权会跳转到这个路径进行授权(power方法的访问路径)private $power_url = 'http://www.wx.com/Oauth.php?method=power';//授权成功回调地址,授权成功后会跳转到这个路径获取用户信息(getUser方法的访问路径)private $redirect_uri = 'http://www.wx.com/Oauth.php?method=getUser';//授权日志目录private $log_filedir = "./logs/oauth/";
//获取用户信息function getUser() {//判断用户session是否存在,存在直接返回[省略]
//没有用户session则去授权获取$this->code = $_GET['code'];$token = $this->getToken();if($token['code']){header('location:' . $this->power_url);//跳转到授权页面}$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$this->token.'&openid='.$this->openid.'&lang=zh_CN';$user = $this->send($url);$this->_log('获取用户信息:' . $this->ajax($user));if(!isset($user['openid'])) {$this->_log('获取用户信息失败!');return "获取信息失败,请重新进入";}
//入库存session[省略]
//返回用户信息return $this->ajax($user);}
//获取tokenpublic function getToken() {$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appID.'&secret='.$this->appsecret.'&code='.$this->code.'&grant_type=authorization_code';$token = $this->send($url);$this->_log('获取access_token:' . $this->ajax($token));if(isset($token['access_token'])) {$this->token = $token['access_token'];$this->openid = $token['openid'];return ['code'=>0, 'msg'=>'获取access_token成功'];}else{return ['code'=>1, 'msg'=>'获取access_token失败'];}}
//网页授权function power() {//请求授权header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appID.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');}
/*** @param $url* @param null $_input* @param string $data_type* @return mixed* $_input= ["post"=>[],"refer"=>"",cookiefile='']*/function send($url, $input=null, $data_type='json') {$ch = curl_init();$useragent = isset($input['useragent']) ? $input['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';curl_setopt( $ch, CURLOPT_URL, $url );curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );curl_setopt( $ch, CURLOPT_AUTOREFERER, true );curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );curl_setopt( $ch, CURLOPT_POST, isset($input['post']) );curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书if( isset($input['post']) ) curl_setopt( $ch, CURLOPT_POSTFIELDS, $input['post'] );if( isset($input['refer']) ) curl_setopt( $ch, CURLOPT_REFERER, $input['refer'] );curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($input['timeout']) ? $input['timeout'] : 5 ) );$result = curl_exec( $ch );curl_close( $ch );if ($data_type == 'json') {$result = json_decode($result,true);}return $result;}
//日志记录private function _log($log_content){ !is_dir($this->log_filedir) && mkdir($this->log_filedir, 0755, true);//目录不存在则创建目录 $log_filename = $this->log_filedir . date("Ymd") . '.log';//日志文件路径
file_put_contents($log_filename, '['.date("H:i:s").']' .PHP_EOL . $log_content . PHP_EOL."------------------------ --------------------------".PHP_EOL, FILE_APPEND);}
function ajax($data){ return json_encode($data, JSON_UNESCAPED_UNICODE);}
}
$oauth = new Oauth();$method = isset($_GET['method'])?$_GET['method']:'getUser';echo $oauth->$method();