yaoruisheng 大佬有话说 : 2020-1-2 14:32:31
百度云加速的api真难用。
本帖最后由 yaoruisheng 于 2020-1-2 14:46 编辑
<?php
baidu_cache_purge(1);
function get_permalink($postid) {
return "https://www.example.com";
}
function baidu_cache_purge($postid) {
$url = ‘https://api.su.baidu.com/v31/yjs/zones/5ddcbff1ffdc7f0fe2a58bb5/purge_cache’;
$headers = array(
‘files’ => get_permalink($postid),
‘X-Auth-Access-Key’ => ‘mdfzr2txy3dx8cpsop1ktbdfg0empqg0’,
‘X-Auth-Nonce’ => random(),
‘X-Auth-Path-Info’ => ‘v31/yjs/zones/5ddcbff1ffdc7f0fe2a58bb5/purge_cache’,
‘X-Auth-Signature-Method’ => ‘HMAC-SHA1’,
‘X-Auth-Timestamp’ => time(),
);
$purgeurl = array(
‘files’ => get_permalink($postid)
);
$postdata = json_encode($purgeurl);
$signed = build_sign(’36ca1f918b8584aebccf48984eef992d’,array_merge($headers,$purgeurl));
$headers[‘X-Auth-Sign’] = $signed;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$f = curl_exec($ch);
curl_close($ch);
echo $f;
}
function build_sign($secret_key , $all_params) {
ksort($all_params);
foreach( $all_params as $key=>$value) {
$str[] = $key .’=’. $value;
}
$str = implode(‘&’,$str);
return base64_encode(hash_hmac(‘sha1’, $str, $secret_key));
}
function random($length = 32, $type = ‘string’, $convert = 0) {
$config = array(
‘number’ => ‘1234567890’,
‘letter’ => ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’,
‘string’ => ‘abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789’,
‘all’ => ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890’
);
if (!isset($config[$type]))
$type = ‘string’;
$string = $config[$type];
$code = ”;
$strlen = strlen($string) – 1;
for ($i = 0; $i < $length; $i++) {
$code .= $string{mt_rand(0, $strlen)};
}
if (!empty($convert)) {
$code = ($convert > 0) ? strtoupper($code) : strtolower($code);
}
return $code;
}
?>
这段代码 始终显示签名错误。。。我是按着文档来的,既然搞不定,无语。
https://su.baidu.com/agency/api.html#/7_kaifazhinan/2_APIcankao-NEW/1_jieruxuzhi/1.7_qianmingsuanfashuoming.md
总算找到了一个php的demo了。。。
https://su.baidu.com/agency/api.html#/7_kaifazhinan/2_APIcankao-NEW/1_jieruxuzhi/1.8_jieruDEMO.md
wangjianjilei 大佬有话说 : 2020-1-2 14:41:35
百度的东西……浪费时间……