<?php
class Wechat {
private $appid = '';
private $secret = '';
private $access_token = '';
private $open_id= '';
private $refresh_token = '';
private $scope = '';
private $unionid ='';
public function __construct($appid,$secret)
{
header("Content-type:text/html;charset=utf-8");
$this->appid = $appid;
$this->secret = $secret;
}
public function debug_set($data)
{
$this->access_token = isset($data['access_token']) ? $data['access_token'] : $this->access_token;
$this->open_id = isset($data['openid']) ? $data['openid'] : $this->open_id;
$this->refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : $this->refresh_token;
$this->scope = isset($data['scope']) ? $data['scope'] : $this->scope;
}
public function set_token($token)
{
$this->access_token = $token;
}
public function codeToToken($type = 'snsapi_base',$redirect = false,$url_='')
{
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
if (!isset($_GET['code']) || $redirect) {
//触发微信返回code码
$baseUrl = urlencode($url_.$_SERVER['REQUEST_URI']);
$url = $this->__CreateOauthUrlForCode($baseUrl,$this->appid,$type);
Header("Location: $url");
exit();
} else {
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secret.'&code='.$_GET['code'].'&grant_type=authorization_code';
$res = $this->curl_get($url);
if (isset($res['access_token'])) {
$this->access_token = $res['access_token'];
$this->open_id = $res['openid'];
$this->refresh_token = $res['refresh_token'];
$this->scope = $res['scope'];
return true;
} else {
//触发微信返回code码
if (isset($_GET['code'])){
$baseUrl = urlencode(str_replace('code='.$_GET['code'],'qwerasdf=a',$url_.$_SERVER['REQUEST_URI']));
} else{
$baseUrl = urlencode($url_.$_SERVER['REQUEST_URI']);
}
$url = $this->__CreateOauthUrlForCode($baseUrl,$this->appid,$type);
Header("Location: $url");
exit();
}
}
}
private function __CreateOauthUrlForCode($redirectUrl,$appid,$type)
{
$urlObj["appid"] = $appid;
$urlObj["redirect_uri"] = "$redirectUrl";
$urlObj["response_type"] = "code";
$urlObj["scope"] = $type;
$urlObj["state"] = "STATE";
$urlObj["connect_redirect"] = "1#wechat_redirect";
$bizString = $this->ToUrlParams($urlObj);
return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}
private function ToUrlParams($urlObj)
{
$buff = "";
foreach ($urlObj as $k => $v)
{
if($k != "sign"){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
public function check_token()
{
$url = 'https://api.weixin.qq.com/sns/auth?access_token='.$this->access_token.'&openid='.$this->open_id;
$res = $this->curl_get($url);
if ($res['errcode'] == '0') {
return true;
} else {
return false;
}
}
public function refresh_token(){
$url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$this->refresh_token;
$res = $this->curl_get($url);
if (isset($res['access_token'])) {
$this->access_token = $res['access_token'];
return true;
} else {
return false;
}
}
public function user_info(){
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$this->access_token.'&openid='.$this->open_id.'&lang=zh_CN';
$res = $this->curl_get($url);
if (isset($res['openid'])) {
$data['code'] = 1;
$data['data'] = $res;
} else {
$data['code'] = 0;
$data['data'] = json_encode($res);
}
return $data;
}
public function curl_get($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json_res = curl_exec($ch);
curl_close($ch);
$array_res = json_decode($json_res, true);
return $array_res;
}
public function get_openid(){
return $this->open_id;
}
public function get_access_token(){
return $this->access_token;
}
}