主要代码
<?php
header("Access-Control-Allow-Origin: *" );
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Access-Control-Allow-Methods: GET,POST,PATCH,PUT,DELETE,OPTIONS");
//print_r($_SERVER['REQUEST_METHOD']);die;
require_once 'Lib_cache.php';
ini_set('date.timezone','Asia/Shanghai');
if($_SERVER['REQUEST_METHOD']=='OPTIONS')
{
$returnData = ['code'=>1,"msg"=>'欢迎光临'];
exit(json_encode($returnData));
}
/**
* Curl请求
* @param string $url
* @return mixed
*/
function GETCURL($url=""){
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//如果成功只将结果返回,不自动输出任何内容。
curl_setopt($curl, CURLOPT_TIMEOUT, 500); //作为最大延续500毫秒,超过这个时间将不去读取页面
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//不检测服务器的证书是否由正规浏览器认证过的授权CA颁发
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//不检测服务器的域名与证书上的是否一致
curl_setopt($curl, CURLOPT_URL, $url);//设置提交地址路径
$res = curl_exec($curl);//执行,并接收返回结果
curl_close($curl);//关闭.
return $res;
}
$appid="xx";
$appSecret="xx";
$goodsid=!empty($_GET['goodsid']) ? $_GET['goodsid'] : 1;
$res = GETCURL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appSecret");
$res = json_decode($res, true);
$token = $res['access_token'];
$accesstoken = $token;
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$accesstoken&type=jsapi";
$jsapi_ticket = Lib_Cache::read("jsapi_tiket", 3600);
if (!$jsapi_ticket || strlen($jsapi_ticket) < 6) {
$res = GETCURL($url);
$res = json_encode($res);
$res = json_decode(json_decode($res));//tiket信息
$jsapi_ticket = $res->ticket;//tiket值
Lib_Cache::write('jsapi_tiket', $jsapi_ticket);
}
if(!empty($jsapi_ticket)){
//开始组合数据
$strdata = array("A","B","C","D","E","F","G","H","I","J","k","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$noncestr= $strdata[rand(0, 14)].$strdata[rand(0, 7)].$strdata[rand(0, 25)].date("Ymdhis").$strdata[rand(7, 25)];
$timestamp= time();
// 注意 URL 一定要动态获取,不能 hardcode.
$urls = "http://xxx/product/".$goodsid;
$strsha1 = "jsapi_ticket=$jsapi_ticket&noncestr=$noncestr×tamp=$timestamp&url=$urls";
//echo $strsha1;
$signature = sha1($strsha1);
$data['noncestr'] = $noncestr;
$data['timestamp'] = $timestamp;
$data['signature'] = $signature;
$data['appid'] = $appid;
$data['jsapi_ticket']= $jsapi_ticket;
$data['url'] = $urls;
$data['strsha1'] = $strsha1;
exit(json_encode($data));
}
//print_r($openId);
?>
缓存类
<?php
class Lib_Cache {
public static function read($file, $expires) {
if(file_exists($file)) {
$time = filemtime($file);
if(time() - $time > $expires) {
return "";
}else {
return file_get_contents($file);
}
}
return null;
}
public static function write($file, $value) {
@file_put_contents($file, $value);
}
public static function write1($file, $value) {
@file_put_contents($file, $value);
}
}
?>