php实现百度云加速API及SDK封装
吾爱主题
阅读:205
2022-11-10 16:15:00
评论:0
百度云加速API参考文档
https://su.baidu.com/help/index.html#/7_kaifazhinan/2_APIcankao-NEW/2_wangzhanjieru/2.1.1_tianjiayuming.md
注意: 官方接口v3和v31有些参数并未实现,或返回的内容和文档描述不符合,所以在封装时交叉使用了2个版本的API,并非码字错漏。
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | /** * 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); } } |
以上就是php实现百度云加速API及SDK封装的详细内容,更多关于php封装百度云加速API及SDK的资料请关注服务器之家其它相关文章!
原文链接:https://www.rehiy.com/post/159
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。