/**
* 获取微信用户信息
* @author: Lucky hypo
*/
header("Content-type: text/html; charset=utf-8");
define('APPID', "wx74702adfdc3b62ca");
define('APPSECRET', "ce35943077594874a578aaeefdc09573");
class class_weixin{
var $appid = APPID;
var $appsecret = APPSECRET;
//构造函数,获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if ($appid && $appsecret) {
$this->appid = $appid;
$this->appsecret = $appsecret;
}
}
//生成扫码登录的URL
public function qrconnect($redirect_url, $scope, $state = NULL){
// $url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $this->appid . "&redirect_uri=" . urlencode($redirect_url) . "&response_type=code&scope=" . $scope . "&state=" . $state . "#wechat_redirect";
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . APPID. '&redirect_uri=' . urlencode($redirect_url) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
return $url;
}
//生成OAuth2的Access Token
public function oauth2_access_token($code)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $code . "&grant_type=authorization_code";
$res = $this->http_request($url);
return json_decode($res, true);
}
//获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)
public function oauth2_get_user_info($access_token, $openid)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
//HTTP请求(支持HTTP/HTTPS,支持GET/POST)
protected function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
//header("Content-type: text/html; charset=utf-8");
//require_once('wxopen.class.php');
$weixin = new class_weixin();
if (!isset($_GET["code"])){
$redirect_url = ''; //注意这里写你的授权地址即可
$jumpurl = $weixin->qrconnect($redirect_url, "snsapi_base", "123");
header("Location: $jumpurl");
}else {
$oauth2_info = $weixin->oauth2_access_token($_GET["code"]);
$userinfo = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']);
// header("Location: host805101609.s652.pppf.com.cn/wxopen/noticy.php?code={$_GET['code']}&state=123");
var_dump($userinfo);
}
?>