<?php
date_default_timezone_set("PRC");
//微信
$url = $_POST['urll'];//获取当前页面的url,接收请求参数
$root['url'] = $url;
//获取access_token,并缓存
$token_file = fopen("token.txt", "r");
$rs = fgets($token_file);
fclose($token_file);
$attr = explode(',',$rs);
$time2 = time();
$token = $attr[0];
if(intval($time2)-intval($attr[1])>7000) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb59cf3f3811db282&secret=bef9f19a79b93030b9552d0407354e19";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
$output = json_decode($output, true);
$token_file = fopen("token.txt","w");//打开token.txt文件
fwrite($token_file,$output['access_token'].','.time());//重写tken.txt全部内容
fclose($token_file);//关闭文件流
curl_close($curl);
$token = $output['access_token'];
}
//获取jsapi_ticket,并缓存
$jsapi_ticket = fopen("jsapi_ticket.txt", "r");
$rs2 = fgets($jsapi_ticket);
fclose($jsapi_ticket);
$attr2 = explode(',',$rs2);
$time3 = time();
$jssdk = $attr2[0];
if(intval($time3)-intval($attr2[1])>7000) {
$ur = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
$res = file_get_contents($ur);
$res = json_decode($res, true);
$jssdk = $res['ticket'];
$token_file2 = fopen("jsapi_ticket.txt","w");//打开token.txt文件
fwrite($token_file2,$jssdk.','.time());//重写tken.txt全部内容
fclose($token_file2);//关闭文件流
}
$timestamp = time();//生成签名的时间戳
$metas = range(0, 9);
$metas = array_merge($metas, range('A', 'Z'));
$metas = array_merge($metas, range('a', 'z'));
$nonceStr = '';
for ($i=0; $i < 16; $i++) {
$nonceStr .= $metas[rand(0, count($metas)-1)];//生成签名的随机串
}
$string1="jsapi_ticket=".$jssdk."&noncestr=$nonceStr"."×tamp=$timestamp"."&url=$url";
$signature=sha1($string1);
$root['appid'] = 'wxb59cf3f3811db282';
$root['nonceStr'] = $nonceStr;
$root['timestamp'] = $timestamp;
$root['signature'] = $signature;
echo json_encode($root);
?>