php 封装一个sdk,PHP版百度云加速API/SDK封装

/**

* 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')

)));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值