这次提供的是php版本,用于自己的官方网站更新了文章后自动更新cdn加速缓存,直接上代码:
$url = "cdn.baidubce.com"; $params = array( "/news.html" ); $siteurl = "https://www.worldflying.cn"; $needclearurls = array(); foreach($params as $val) { array_push($needclearurls, array("url" => $siteurl.$val)); } $postdata = json_encode(array("tasks" => $needclearurls), JSON_UNESCAPED_SLASHES); $ak = [用户的accesskey]; $sk = [用户的secretkey]; $time = gmdate("Y-m-d\TH:i:s\Z"); $expire = 300; $authstringprefix = "bce-auth-v1/".$ak."/".$time."/".$expire; $canonicaluri = "/v2/cache/purge"; $canonicalquerystring = ""; $canonicalheaders = "host:".$url; $canonicalrequest = "POST\n".$canonicaluri."\n".$canonicalquerystring."\n".$canonicalheaders; $signedheaders = "host"; $signingkey = hash_hmac("sha256", $authstringprefix, $sk); $signature = hash_hmac("sha256", $canonicalrequest, $signingkey); $headers = array("Authorization: bce-auth-v1/".$ak."/".$time."/".$expire."/".$signedheaders."/".$signature); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://".$url.$canonicaluri); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $ret = curl_exec($ch); curl_close($ch); echo $ret;
文章作者:沃航科技