public function auth_loginWay()
{
$scope = 'snsapi_userinfo';
$appid = ''; //appid
$state = '';
// $callback = urlencode(DOMAIN.'/car_api/v1/index.php?app=car_active&way=auth_info' . http_build_query([
// 'oauth_type' => 'user_oauth',
// 'scope' => $scope,
// 'wx_appid' => $appid
// ]));
$callback = urlencode(DOMAIN.'/car_api/v1/index.php?' . http_build_query([
'app' => 'car_active',
'way' => 'auth_info'
]));
redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$callback}&response_type=code&scope={$scope}&state={$state}#wechat_redirect");
}
public function auth_infoWay()
{
$code = $_GET['code'];
$state = $_GET['state'];
if(empty($code)){
output_error('code为空');
}
if(empty($state)){
output_error('state为空');
}
if($state != md5($this->appid . '_' . $this->appsecret)){
output_error('验证错误');
}
$wx = new Wechat();
//微信网页授权
$oauth_info = $wx->getOauthInfo($code);
$oauth = json_decode($oauth_info, TRUE);
//全局access_token
$access_token = $wx->get_access_token();
//用户信息
$user_info_json = $wx->getOauthUserInfo($access_token, $oauth['openid']);
$user_info = json_decode($user_info_json, TRUE);
$datas['nickname'] = $user_info['nickname'];
$datas['headimgurl'] = $user_info['headimgurl'];
output_data($datas);
}
<?php
defined('In33hao') or exit('Access Invalid!');
/**
* 微信分享逻辑
*/
class Wechat
{
public $appid='1';
public $appsecret='1';
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appid,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
private function getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
if (file_exists("jsapi_ticket.json")) {
$data = json_decode(file_get_contents("jsapi_ticket.json"),true);
if ($data['expire_time'] < time()) {
$accessToken = $this->get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data['expire_time'] = time() + 7000;
$data['jsapi_ticket'] = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$ticket = $data['jsapi_ticket'];
}
}else{
$accessToken = $this->get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data['expire_time'] = time() + 7000;
$data['jsapi_ticket'] = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}
return $ticket;
}
public function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
// 获取access_token
public function get_access_token(){
$file='access_token.json';
if (file_exists($file)) {
//读取access_token文件内容
$data = json_decode(file_get_contents($file),true);
//判断access_token是否过期
if(time() - filemtime($file) < $data['expires_in']){
return $data["access_token"];
}else{
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
$httpdata=$this->httpGet($url);
$data=json_decode($httpdata,true);
//保存Access Token字符串到指定文件中
file_put_contents($file, $httpdata);
return $data['access_token'];
}
}else{
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
$httpdata=$this->httpGet($url);
$data=json_decode($httpdata,true);
//保存Access Token字符串到指定文件中
file_put_contents($file, $httpdata);
return $data['access_token'];
}
}
//获取用户 openid
public function getOauthInfo($code)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
$response = $this->httpGet($url);
return $response;
}
//获取用户信息
public function getOauthUserInfo($accesstoken, $openid)
{
$apiurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$accesstoken}&openid={$openid}&lang=zh_CN";
$response = $this->httpGet($apiurl);
return $response;
}
}