您好,登錄后才能下訂單哦!
這篇“php如何實(shí)現(xiàn)百度云加速API及SDK封裝”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“php如何實(shí)現(xiàn)百度云加速API及SDK封裝”文章吧。
注意: 官方接口v3和v31有些參數(shù)并未實(shí)現(xiàn),或返回的內(nèi)容和文檔描述不符合,所以在封裝時(shí)交叉使用了2個(gè)版本的API,并非碼字錯(cuò)漏。
/** * Author: rehiy <https://www.rehiy.com> * Update: 2021-04-12 */ class Yunjiasu { private $su; private $name = ''; private $zone = []; private $zone_fn = [ 'dns_records', 'dns_records_post', 'dns_records_patch', 'dns_records_delete', 'purge_cache', ]; public function __construct($domain, $access_key, $secret_key) { $this->su = new YunjiasuCore($access_key, $secret_key); $this->zone = $this->su->zones(['name' => $domain]); if (empty($this->zone['id'])) { throw new Exception('not found domain'); } $this->name = $domain; } public function __call($name, $arguments) { if (in_array($name, $this->zone_fn)) { array_unshift($arguments, $this->zone['id']); } return call_user_func_array(array($this->su, $name), $arguments); } public function purge_cache($data) { return $this->su->purge_cache($this->zone['id'], $data); } public function dns_records($data = []) { $list = $this->su->dns_records($this->zone['id']); if (empty($list) || empty($data)) { return $list; } return array_filter( $list, function ($item) use ($data) { isset($data['name']) && $data['name'] .= '.' . $this->name; return count($data) === count(array_intersect_assoc($item, $data)); } ); } public function dns_records_delete($data) { return array_map( function ($rs) { return $this->su->dns_records_delete($this->zone['id'], $rs['id']); }, $this->dns_records($data) ); } } class YunjiasuCore { private $api_base = 'https://api.su.baidu.com/'; private $access_key = ''; private $secret_key = ''; public function __construct($access_key, $secret_key) { $this->access_key = $access_key; $this->secret_key = $secret_key; } //////////////////////////////////////////////////////////////// public function zones($data) { $path = 'v31/yjs/zones'; return $this->api_call('GET', $path, $data); } //////////////////////////////////////////////////////////////// public function dns_records($zone_id) { $path = 'v31/yjs/zones/' . $zone_id . '/dns_records'; return $this->api_call('GET', $path); } public function dns_records_post($zone_id, $data) { $path = 'v31/yjs/zones/' . $zone_id . '/dns_records'; return $this->api_call('POST', $path, $data); } public function dns_records_patch($zone_id, $data) { $path = 'v31/yjs/zones/' . $zone_id . '/dns_records'; return $this->api_call('PATCH', $path, $data); } public function dns_records_delete($zone_id, $id) { $path = 'v31/yjs/zones/' . $zone_id . '/dns_records/' . $id; return $this->api_call('DELETE', $path); } //////////////////////////////////////////////////////////////// public function purge_cache($zone_id, $data) { $path = 'v31/yjs/zones/' . $zone_id . '/purge_cache'; return $this->api_call('DELETE', $path, $data); } //////////////////////////////////////////////////////////////// public function custom_certificates($data) { $path = 'v3/yjs/custom_certificates'; return $this->api_call('GET', $path, $data); } public function custom_certificates_post($data) { $path = 'v3/yjs/custom_certificates'; return $this->api_call('POST', $path, $data); } public function custom_certificates_delete($data) { $path = 'v3/yjs/custom_certificates'; return $this->api_call('DELETE', $path, $data); } //////////////////////////////////////////////////////////////// private function api_call($method, $path, $data = NULL) { if (PHP_SAPI == 'cli') { print_r("\n> " . $method . ' /' . $path); } $url = $this->api_base . $path; $header = $this->api_header($path, $data); $result = $this->http_repuest($method, $url, $header, $data); if (!empty($result['errors'])) { $error = json_encode($result['errors']); throw new Exception($error); } if (!empty($result['result'])) { return $result['result']; } if (!empty($result['success'])) { return ['success' => true]; } 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); } }
以上就是關(guān)于“php如何實(shí)現(xiàn)百度云加速API及SDK封裝”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。