/**
* Author: anrip
* Update: 2020-11-19
*/
class Yunjiasu
{
private $api_base = 'https://api.su.baidu.com/';
private $access_key = '1234567';
private $secret_key = '1234567';
public function zones($data)
{
$path = 'v31/yjs/zones';
return $this->api_call('GET', $path, $data);
}
public function purge_cache($id, $data)
{
$path = 'v31/yjs/zones/' . $id . '/purge_cache';
return $this->api_call('DELETE', $path, $data);
}
private function api_call($method, $path, $data = NULL)
{
$url = $this->api_base . $path;
$header = $this->api_header($path, $data);
$result = $this->http_repuest($method, $url, $header, $data);
if (!empty($result['errors'])) {
$error = array_pop($result['errors']);
$error['error'] = 1;
return $error;
}
if (!empty($result['result'])) {
return $result['result'];
}
if (!empty($result['success'])) {
return ['success' => 1];
}
return $result;
}
private function api_header($path, $data = NULL)
{
$params = [
'X-Auth-Access-Key' => $this->access_key,
'X-Auth-Nonce' => uniqid(),
'X-Auth-Path-Info' => $path,
'X-Auth-Signature-Method' => 'HMAC-SHA1',
'X-Auth-Timestamp' => time(),
];
if (is_array($data)) {
$params = array_merge($params, $data);
}
ksort($params);
$header = $signls = [];
foreach ($params as $k => $v) {
if (is_bool($v)) {
$v = $v ? 'true' : 'false';
}
if (is_array($v)) {
$v = str_replace('","', '", "', json_encode($v, JSON_UNESCAPED_SLASHES));
}
if (strpos($k, 'X-Auth') === 0) {
$header[] = $k . ':' . $v;
}
if ($v !== '') {
$signls[] = $k . '=' . $v;
}
}
$header[] = 'X-Auth-Sign:' . base64_encode(
hash_hmac('sha1', implode('&', $signls), $this->secret_key, true)
);
return $header;
}
private function http_repuest($method, $url, $header = NULL, $body = NULL)
{
$ch = curl_init();
if ($method == 'GET' && $body) {
$url .= '?' . http_build_query($body);
$body = NULL;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if ($body) {
if (is_array($body)) {
$body = json_encode($body);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$result = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) {
return ['error' => $errno, 'message' => $error];
}
return json_decode($result, true);
}
}
#######################################################################
$su = new Yunjiasu();
$rs_zones = $su->zones(array(
'name' => 'anrip.com'
));
if (isset($rs_zones['error'])) {
exit(json_encode($rs_zones));
}
print_r($su->purge_cache($rs_zones['id'], array(
'files' => array('https://www.anrip.com/test.jpg')
)));