phpcms站点用php脚本自动刷新腾讯云CDN
Author:zhoulujun Date:
到此,网站运行对基本问题解决了。但是,网站,添加内容,因为本站首页和列表也都是没有生成静态html,而是动态php链接,在CDN把php文件 缓存时间设置为0,即可。
但是,网站内容更新,CDN无法同步。
百度搜索下相关教程,发现只有wordpress的。腾讯云官方 api说明也对参数做相关详细解释。点击到官方提供对demo文件里面,才知道个大概。于是新对折腾就此开始。其中细节不再陈述(too sample……),show code
在 extention.func.php 文件里面加上(直接复制代码即可
$api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $bdurls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result = json_decode($result, 1);
return $result;
}
/**
* 刷新DNS
* @param {Array} $bdurls
* @return result
*/
function refresh_CDN($bdurls)
{
/*需要填写你的密钥,可从 https://console.qcloud.com/capi 获取 SecretId 及 $secretKey*/
$secretKey = '“your key';
$secretId = "your id";
$action = 'RefreshCdnUrl';
/*****************参数************************/
/**
* 参数名 类型 是否必填 描述
* urls array 是 刷新的目录
**/
/*参数转换,放在这里其实不应该*/
$PRIVATE_PARAMS = array();
foreach ($bdurls as $k => $v) {
$PRIVATE_PARAMS["urls." . $k] = $v;
}
var_export($PRIVATE_PARAMS);
$HttpUrl = "cdn.api.qcloud.com";
/*除非有特殊说明,如MultipartUploadVodFile,其它接口都支持GET及POST*/
$HttpMethod = "POST";
/*是否https协议,大部分接口都必须为https,只有少部分接口除外(如MultipartUploadVodFile)*/
$isHttps = true;
/*下面这五个参数为所有接口的 公共参数;对于某些接口没有地域概念,则不用传递Region(如DescribeDeals)*/
$COMMON_PARAMS = array(
'Nonce' => rand(),
'Timestamp' => time(NULL),
'Action' => $action,
'SecretId' => $secretId,
);
/***********************************************************************************/
function CreateRequest($HttpUrl, $HttpMethod, $COMMON_PARAMS, $secretKey, $PRIVATE_PARAMS, $isHttps)
{
$FullHttpUrl = $HttpUrl . "/v2/index.php";
/***************对请求参数 按参数名 做字典序升序排列,注意此排序区分大小写*************/
$ReqParaArray = array_merge($COMMON_PARAMS, $PRIVATE_PARAMS);
ksort($ReqParaArray);
/**********************************生成签名原文**********************************
* 将 请求方法, URI地址,及排序好的请求参数 按照下面格式 拼接在一起, 生成签名原文,此请求中的原文为
* GETcvm.api.qcloud.com/v2/index.php?Action=DescribeInstances&Nonce=345122&Region=gz
* &SecretId=AKIDz8krbsJ5yKBZQ ·1pn74WFkmLPx3gnPhESA&Timestamp=1408704141
* &instanceIds.0=qcvm12345&instanceIds.1=qcvm56789
* ****************************************************************************/
$SigTxt = $HttpMethod . $FullHttpUrl . "?";
$isFirst = true;
foreach ($ReqParaArray as $key => $value) {
if (!$isFirst) {
$SigTxt = $SigTxt . "&";
}
$isFirst = false;
/*拼接签名原文时,如果参数名称中携带_,需要替换成.*/
if (strpos($key, '_')) {
$key = str_replace('_', '.', $key);
}
$SigTxt = $SigTxt . $key . "=" . $value;
}
/*********************根据签名原文字符串 $SigTxt,生成签名 Signature******************/
$Signature = base64_encode(hash_hmac('sha1', $SigTxt, $secretKey, true));
/***************拼接请求串,对于请求参数及签名,需要进行urlencode编码********************/
$Req = "Signature=" . urlencode($Signature);
foreach ($ReqParaArray as $key => $value) {
$Req = $Req . "&" . $key . "=" . urlencode($value);
}
/*********************************发送请求********************************/
if ($HttpMethod === 'GET') {
if ($isHttps === true) {
$Req = "https://" . $FullHttpUrl . "?" . $Req;
} else {
$Req = "http://" . $FullHttpUrl . "?" . $Req;
}
$Rsp = file_get_contents($Req);
} else {
if ($isHttps === true) {
$Rsp = SendPost("https://" . $FullHttpUrl, $Req, $isHttps);
} else {
$Rsp = SendPost("http://" . $FullHttpUrl, $Req, $isHttps);
}
}
var_export(json_decode($Rsp, true));
return json_decode($Rsp, true);
}
function SendPost($FullHttpUrl, $Req, $isHttps)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Req);
curl_setopt($ch, CURLOPT_URL, $FullHttpUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($isHttps === true) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$result = curl_exec($ch);
return $result;
}
return CreateRequest($HttpUrl, $HttpMethod, $COMMON_PARAMS, $secretKey, $PRIVATE_PARAMS, $isHttps);
}
/**
* 360推送 @www.zhoulujun.cn
*/
function getPushSoUrl($url)
{
$token = "your token";
return $str = "https://s.360.cn/so/zz.gif?url=" . urlencode($url) . "&sid=" . $token . "&token=" . getPushToken($url, $token);
}
function getPushToken($url, $o)
{
$n = str_split($url, 1);
$n = array_reverse($n);
$r = str_split($o, 1);
$i = [];
for ($s = 0, $o = 16; $s token=$token;
}
// function __construct($token) {
// $this->token=$token;
// }
public function getPushToken($url,$o){
$n = str_split($url,1);
$n=array_reverse($n);
$r = str_split($o,1);
$i=[];
for($s=0,$o=16;$stoken."&token=".$this->getPushToken($url,$this->token);
}
}
?>
然后在phpcms/modules/contetn/create_html.php
if($content_ishtml) {
$this->url = pc_base::load_app_class('url');
$this->db->set_model($modelid);
if(empty($_POST['ids'])) showmessage(L('you_do_not_check'));
$this->html = pc_base::load_app_class('html');
$ids = implode(',', $_POST['ids']);
$rs = $this->db->select("catid='$catid' AND id IN ($ids)");
$tablename = $this->db->table_name.'_data';
foreach($rs as $r) {
if($r['islink']) continue;
$this->db->table_name = $tablename;
$r2 = $this->db->get_one(array('id'=>$r['id']));
if($r2) $r = array_merge($r,$r2);
//判断是否为升级或转换过来的数据
if(!$r['upgrade']) {
$urls = $this->url->show($r['id'], '', $r['catid'],$r['inputtime']);
} else {
$urls[1] = $r['url'];
}
if( strpos($r['url'],SITE_URL)){
$bdurls[] = $r['url'];
}else{
$bdurls[] = SITE_PROTOCOL.SITE_URL.$r['url'];
}
$this->html->show($urls[1],$r,0,'edit',$r['upgrade']);
}
$push_result = push_baidu($bdurls);
$refresh_result= refresh_CDN($bdurls);
$msg = '';
if($push_result['success'] < 1){
$msg = '百度联盟推送链接失败!';
}
if($refresh_result['codeDesc']!=="Success"){
$msg=$msg."CDN推送失败:".$refresh_result["codeDesc"];
}
showmessage(L('operation_success').$msg,HTTP_REFERER);
最后在phpcms/model/content_model.class.php
//生成静态
if(!$isimport && $data['status']==99) {
//百度推送
$bdurls[] = SITE_PROTOCOL.SITE_URL.$urls[1];
$push_result = push_baidu($bdurls);
refresh_CDN($bdurls);
$msg = '';
if ($push_result['success'] index();
if(defined('RELATION_HTML')) $html->create_relation_html($catid);
}
和此处替换
//生成静态
if(!$isimport && $data['status']==99) {
//百度推送
$bdurls[] = SITE_PROTOCOL.SITE_URL.$urls[1];
$push_result = push_baidu($bdurls);
refresh_CDN($bdurls);
$msg = '';
if ($push_result['success'] index();
if(defined('RELATION_HTML')) $html->create_relation_html($catid);
}
至此,上传替换文件,okay!
转载本站文章《phpcms站点用php脚本自动刷新腾讯云CDN》,
请注明出处:https://www.zhoulujun.cn/html/php/phpcms/2018_0710_8128.html