本帖最后由 cnhong 于 2020-9-18 18:42 编辑
稍微加了点东西
支持验证TOKEN[PHP] 纯文本查看 复制代码 <?php
header("Content-type:application/json");
// 声明APPID、APPSECRET
define("APPID", "wxxxxxb4a7e599xxxx");
define("SECRET", "9a01e1xxxx1377c13acxxxxc87c167de");
//校验用的TOKEN
define("TOKEN", "cnhong2020");
//校验
if (isset($_GET['echostr']))
{
Valid::exec();
}
// 获取access_token和jsapi_ticket
function getToken(){
$file = @file_get_contents("access_token.json",true);//读取access_token.json里面的数据
$result = json_decode($file,true);
//判断access_token是否在有效期内,如果在有效期则获取缓存的access_token
//如果过期了则请求接口生成新的access_token并且缓存access_token.json
if (!isset($result['expires']) || time() > $result['expires']){
$data = array();
$data['access_token'] = getNewToken();
$data['expires'] = time()+7000;
$jsonStr = json_encode($data);
[size=14px] file_put_contents("access_token.json",$jsonStr);[/size] return $data['access_token'];
}else{
return $result['access_token'];
}
}
//生成json
function msg($result, $msg, $dwz=""){
$array = array(
"result" => $result,
"msg" => $msg,
"dwz" => $dwz
);
return json_encode($array,JSON_UNESCAPED_UNICODE);
}
//获取新的access_token
function getNewToken(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . SECRET;
$access_token_json = (new Curl($url))->exec();
$token_arr = json_decode($access_token_json, true);
//获取access_token失败
if(!isset($token_arr["access_token"])){
echo msg(302, "获取access_token失败!");
exit;
}
return $token_arr["access_token"];
}
// 获得长链接
$long_url = trim($_GET["long_url"]);
// 过滤
if (empty($long_url)) {
echo msg(101, "请传入长链接");
} else if (strpos($long_url,'http') !== false){
$url = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token='.getToken();
$postData = array(
'action'=>'long2short',
'long_url'=>$long_url
);
//获取数据
$curl = new Curl($url, $postData);
$dwzStr = $curl->exec();
//解析数据
$arr_dwzStr = json_decode($dwzStr, true);
//print_r($arr_dwzStr);
//判断是否出错
if($arr_dwzStr["errcode"]==0){
$dwz = $arr_dwzStr["short_url"];
// 返回结果
echo msg(200, "生成成功", $dwz);
}else{
echo msg(301, "生成失败");
}
}else{
echo msg(103, "长链接不合法");
}
//curl类
class Curl{
private $ch;
function __construct($url, $postdata=''){
$this->ch = curl_init();
//请求地址
curl_setopt($this->ch, CURLOPT_URL, $url);
//post数据
if(is_array($postdata)){
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($postdata));
}
// 验证HTTPS证书
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == 'https' ) {
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
}
//获取的信息以文件流的形式返回,而不是直接输出
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
}
public function exec(){
$reponse = curl_exec($this->ch);
curl_close($this->ch);
return $reponse;
}
}
//校验类
class Valid{
public static function exec()//校验-外部调用
{
$echoStr = $_GET["echostr"];
if(self::checkSignature()){
echo $echoStr;
}
exit;
}
private static function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?> |