微信官方包代碼修改
獲取openid州刽,type為openid獲取方式
public function GetOpenid($type)
{
//通過(guò)code獲得openid
if (!isset($_GET['code'])){
//觸發(fā)微信返回code碼
$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$url = $this->_CreateOauthUrlForCode($baseUrl,$type);
Header("Location: $url");
exit();
} else {
//獲取code碼空执,以獲取openid
$code = $_GET['code'];
$openid = $this->getOpenidFromMp($code);
return $openid;
}
}
這里傳入type,區(qū)分授權(quán)方式
private function _CreateOauthUrlForCode($redirectUrl,$type)
{
$config = new WxPayConfig();
$urlObj["appid"] = $config->GetAppId();
$urlObj["redirect_uri"] = "$redirectUrl";
$urlObj["response_type"] = "code";
$urlObj["scope"] = $type;
$urlObj["state"] = "STATE"."#wechat_redirect";
$bizString = $this->ToUrlParams($urlObj);
return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}
public function GetOpenidFromMp($code)
{
$url = $this->__CreateOauthUrlForOpenid($code);
//初始化curl
$ch = curl_init();
$curlVersion = curl_version();
$config = new WxPayConfig();
$ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
.$config->GetMerchantId();
//設(shè)置超時(shí)
//curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$proxyHost = "0.0.0.0";
$proxyPort = 0;
$config->GetProxy($proxyHost, $proxyPort);
if($proxyHost != "0.0.0.0" && $proxyPort != 0){
curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
}
//運(yùn)行curl穗椅,結(jié)果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出openid
$data = json_decode($res,true);
$this->data = $data;
$openid = $data;
return $openid;
}
獲取openid
public function getOpenids()
{
$obj = new \JsApiPay();
$data = $obj->GetOpenid("snsapi_userinfo");
return $data;
}
public function getOpenid()
{
session_start();
$obj = new \JsApiPay();
$data = $obj->GetOpenid("snsapi_base");
$_SESSION['openid'] = $data['openid'];
}
curlGet
public static function curlGet($url = '', $options = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https請(qǐng)求 不驗(yàn)證證書和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
獲取access_token并保存至數(shù)據(jù)庫(kù)維護(hù)
public function getWxAccessToken()
{
$access_token = db('access_token')->where("id>0")->find();
if ($access_token) {
$endtime = $access_token['inserttime'] + 7000;
if ($access_token['access_token'] && $endtime > time()) {
//如果access_token在session沒有過(guò)期
return $access_token['access_token'];
} else {
//如果access_token比存在或者已經(jīng)過(guò)期脆烟,重新取access_token
//1 請(qǐng)求url地址
$config = new \WxPayConfig();
$AppId = $config->GetAppId();
$AppSecret = $config->GetAppSecret();
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppId . "&secret=" . $AppSecret;
$res = $this->http_curl($url, 'get', 'json');
$res = json_decode($res, true);
$access_token = $res['access_token'];
//將重新獲取到的aceess_token存到session
$update_data = [
'inserttime' => time(),
'access_token' => $access_token
];
$update_data = db('access_token')->where("id>0")->update($update_data);
return $access_token;
}
} else {
$config = new \WxPayConfig();
$AppId = $config->GetAppId();
$AppSecret = $config->GetAppSecret();
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $AppId . "&secret=" . $AppSecret;
$res = $this->http_curl($url, 'get', 'json');
$res = json_decode($res, true);
$access_token = $res['access_token'];
//將重新獲取到的aceess_token存到session
$update_data = [
'inserttime' => time(),
'access_token' => $access_token
];
$update_data = db('access_token')->insert($update_data);
return $access_token;
}
}
獲取用戶信息
public function getUserdata($openid,$access_token)
{
$response = self::curlGet('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
return json_decode($response,true);
}
將用戶信息存入session并返回
public function getuser()
{
session_start();
$data = $this->getOpenids();
$openid = $data['openid'];
$access_token = $data['access_token'];
$data = $this->getUserdata($openid,$access_token);
$_SESSION['user'] = $data;
$this->redirect('調(diào)回路由');
}
獲取公眾號(hào)微信config
public function getwxconfig()
{
//session_start();
$token = $this->getWxAccessToken();
$urls = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
//https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
$jsapi = file_get_contents($urls);
$arr = json_decode($jsapi, true);
//獲取jsapi_ticket
$jsapi_ticket = $arr['ticket'];
//隨機(jī)字符串
$noncestr = $this->getRandChar(16);
//獲取時(shí)間戳
$timestamp = strtotime(date("Y-m-d H:i:s"));
//獲取url
$config = new \WxPayConfig();
$AppId = $config->GetAppId();
$url = input('url');
//$data = array("jsapi_ticket"=>$jsapi_ticket,"noncestr"=>$noncestr,"timestamp"=>$timestamp,"url"=>$url);
$sha1 = sha1("jsapi_ticket=$jsapi_ticket&noncestr=$noncestr×tamp=$timestamp&url=$url");
$arr = array('appid' => $AppId, 'noncestr' => $noncestr, 'timestamp' => $timestamp, 'sha1' => $sha1);
return json($arr);
}
微信字符串
public function getRandChar($length)
{
$str = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol) - 1;
for ($i = 0; $i < $length; $i++) {
$str .= $strPol[rand(0, $max)];//rand($min,$max)生成介于min和max兩個(gè)數(shù)之間的一個(gè)隨機(jī)整數(shù)
}
return $str;
}
初始化設(shè)置
public function configall()
{
$this->getOpenid();//靜默獲取openid
$this->getwxconfig();//獲取wxconfig參數(shù)
$this->redirect("跳轉(zhuǎn)路由");
}