public function wx_Qr_code_login(){
$scene_str = 'tx'.time();
$res = loginBusiness::wx_Qr_code($scene_str);
if ($res){
return show('200','微信二維碼',$res);
}
}
public static function wx_Qr_code($scene_str){
$access_token = login::get_access_token();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$data = array(
"expire_seconds" => 60,
"action_name" => "QR_STR_SCENE",
"action_info" => array("scene" => array("scene_str" => $scene_str)),
);
$result = json_decode(login::httpRequest($url, json_encode($data)),true);
$img = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=". $result['ticket'];
$result = [
'img'=>$img,
'ticket'=>$scene_str
];
return $result;
}
public static function get_access_token($token_file='./access_token'){
$life_time = 3600;
if (file_exists($token_file) && time() - filemtime($token_file) < $life_time) {
return file_get_contents($token_file);
}
$appid = config('wechat.appid');
$secret = config('wechat.secret');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$access_token = json_decode(login::httpRequest($url),true);
$access_token = $access_token['access_token'];
file_put_contents($token_file, $access_token);
return $access_token;
}
private static function httpRequest($url, $data = ""){
$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;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結