include('./Card/Common/class_weixin_adv.php');
$weixin=new class_weixin_adv("appid", "secret");
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code"; $res = $weixin->https_request($url);//调用SDK方法获取到res 从中可以得到openid $res=(json_decode($res, true));//转换成array 方便调用openid
$row=$weixin->get_user_info($res['openid']);
public function oauth2(){
include('./Card/Common/class_weixin_adv.php');
$weixin=new class_weixin_adv("appid", "secret");
if (isset($_GET['code'])){
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$_GET['code']."&grant_type=authorization_code";
$res = $weixin->https_request($url);
$res=(json_decode($res, true));
$row=$weixin->get_user_info($res['openid']);
if ($row['openid']) {
//这里写上逻辑,存入cookie,数据库等操作
cookie('weixin',$row['openid'],25920);
}else{
$this->error('授权出错,请重新授权!');
}
}else{
echo "NO CODE";
}
$this->display();
}
<?php
/**
* 微信SDK
* pan041ymail@gmail.com
*/
class class_weixin_adv
{
var $appid = "";
var $appsecret = "";
//构造函数,获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid){
$this->appid = $appid;
}
if($appsecret){
$this->appsecret = $appsecret;
}
$this->lasttime = 1395049256;
$this->access_token = "nRZvVpDU7LxcSi7GnG2LrUcmKbAECzRf0NyDBwKlng4nMPf88d34pkzdNcvhqm4clidLGAS18cN1RTSK60p49zIZY4aO13sF-eqsCs0xjlbad-lKVskk8T7gALQ5dIrgXbQQ_TAesSasjJ210vIqTQ";
if (time() > ($this->lasttime + 7200)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->https_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$this->lasttime = time();
}
}
//获取用户基本信息
public function get_user_info($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
$res = $this->https_request($url);
return json_decode($res, true);
}
//https请求
public function https_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, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有