api.php
<?php
function isAjax(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
if(!isAjax()) exit;
// if(!isset($_SERVER["HTTP_REFERER"])) exit;
include_once('./config.php');
include_once('./lib/Weixin.class.php');
$url = urldecode($_GET["url"]);
$weixin = new Weinxin(APP_ID, APP_SECRET, DATA_PATH);
$sign = $weixin->create_signature($url);
echo $sign;
config.php
<?php
define('APP_ID', 'wxxxxxxxxxxxx');
define('APP_SECRET', '6b6xxxxxxxxxxxx');
define('DATA_PATH', './data/');
lib/Weixin.class.php
<?php
/**
* Class Weixin
* @description 微信公众平台获取accessToken以及jssdk签名的简易接口类
* @author liveidzong@gmail.com
* @url https://zongliwei.com
* @usage 见api.php
*/
class Weinxin{
const TOKEN_API = 'https://api.weixin.qq.com/cgi-bin/token';
const TICKET_API = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
protected $app_id;
protected $app_secret;
protected $data_path;
function __construct($app_id, $app_secret, $data_path){
$this->app_id = $app_id;
$this->app_secret = $app_secret;
$this->data_path = $data_path;
}
public function get_time_stamp(){
return (new DateTime('now'))->getTimestamp();
}
public function curl_http($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$res = curl_exec($curl);
return $res;
}
//返回数组
public function get_access_token(){
$now = $this->get_time_stamp();
$paramArray = array(
'grant_type' => 'client_credential',
'appid' => $this->app_id,
'secret' => $this->app_secret
);
$url = self::TOKEN_API. '?' . http_build_query($paramArray);
$result = json_decode($this->curl_http($url), true);
if( isset($result['errcode'])) {
return 0;
}else{
return array_merge($result, array('update_at' => $now));
}
}
//返回数组
public function get_jssdk_ticket() {
$now = $this->get_time_stamp();
$token = $this->get_access_token_from_local();
$paramArray = array(
'access_token' => $token['access_token'],
'type' => 'jsapi'
);
$url = self:: TICKET_API . '?'. http_build_query($paramArray);
$result = json_decode($this->curl_http($url), true);
if($result['errcode']) {
return 0;
}else{
return array_merge($result, array('update_at' => $now));
}
}
//$url 从api中传过来, 返回json对象
public function create_signature($url) {
$timestamp = $this->get_time_stamp();
$token_get = $this->get_access_token_from_local();
$token = $token_get['access_token'];
$ticket = $this->get_jssdk_ticket_from_local();
$jssdk_ticket = $ticket['ticket'];
$nonceStr = $this->get_random_str();
$string = "jsapi_ticket=$jssdk_ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array (
"appId" => $this->app_id,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string,
"ticket" => $jssdk_ticket,
"token" => $token
);
//返回json对象
return json_encode($signPackage);
}
//返回数组
public function get_access_token_from_local() {
$now = $this->get_time_stamp();
$cache_path = $this->data_path . 'ACCESS_TOKEN.json';
if(file_exists($cache_path)) {
$cache = file_get_contents($cache_path);
if($cache) {
$cache = json_decode($cache, true);
if($cache['update_at'] + $cache['expires_in'] > $now) {
return $cache;
}else{
$latest_token = $this->get_access_token();
if($latest_token) {
$fp = fopen($cache_path,w);
//默认是fugai
fwrite($fp, json_encode($latest_token));
fclose($fp);
return $latest_token;
}else{
return 0;
}
}
}else{
$latest_token = $this->get_access_token();
if($latest_token) {
$fp = fopen($cache_path, w);
//默认是fugai
fwrite($fp, json_encode($latest_token));
fclose($fp);
return $latest_token;
}else{
return 0;
}
}
}
}
public function get_jssdk_ticket_from_local(){
$now = $this->get_time_stamp();
$cache_path = $this->data_path . 'JSAPI_TICKET.json';
if(file_exists($cache_path)) {
$cache = file_get_contents($cache_path);
if($cache) {
$cache = json_decode($cache, true);
if($cache['update_at'] + $cache['expires_in'] > $now) {
return $cache;
}else{
echo $cache_path;
$latest_jsdsk_ticket = $this->get_jssdk_ticket();
if($latest_jsdsk_ticket) {
$fp = fopen($cache_path, w);
//默认是覆盖
fwrite($fp, json_encode($latest_jsdsk_ticket));
fclose($fp);
return $latest_jsdsk_ticket;
}else{
return 0;
}
}
}else{
$latest_jsdsk_ticket = $this->get_jssdk_ticket();
if($latest_jsdsk_ticket) {
$fp = fopen($cache_path, w);
//默认是覆盖
fwrite($fp, json_encode($latest_jsdsk_ticket));
fclose($fp);
return $latest_jsdsk_ticket;
}else{
return 0;
}
}
}
}
public function get_random_str() {
$str = "";
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($str_pol) - 1;
for ($i = 0; $i < 16; $i++) {
$str .= $str_pol[mt_rand(0, $max)];
}
return $str;
}
}
本文由 dealdot <dealdot#163.com> 创作, Full Stack Developer @ DeepBlue
本文最后编辑时间为: May 14, 2021 at 19:11 pm
转载请注明来源