CdnManager.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Qiniu\Cdn;
  3. use Qiniu\Auth;
  4. use Qiniu\Http\Error;
  5. use Qiniu\Http\Client;
  6. use Qiniu\Http\Proxy;
  7. final class CdnManager
  8. {
  9. private $auth;
  10. private $server;
  11. private $proxy;
  12. public function __construct(Auth $auth, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
  13. {
  14. $this->auth = $auth;
  15. $this->server = 'http://fusion.qiniuapi.com';
  16. $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
  17. }
  18. /**
  19. * @param array $urls 待刷新的文件链接数组
  20. * @return array
  21. */
  22. public function refreshUrls(array $urls)
  23. {
  24. return $this->refreshUrlsAndDirs($urls, array());
  25. }
  26. /**
  27. * @param array $dirs 待刷新的文件链接数组
  28. * @return array
  29. * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
  30. * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
  31. */
  32. public function refreshDirs(array $dirs)
  33. {
  34. return $this->refreshUrlsAndDirs(array(), $dirs);
  35. }
  36. /**
  37. * @param array $urls 待刷新的文件链接数组
  38. * @param array $dirs 待刷新的目录链接数组
  39. *
  40. * @return array 刷新的请求回复和错误,参考 examples/cdn_manager.php 代码
  41. * @link http://developer.qiniu.com/article/fusion/api/refresh.html
  42. *
  43. * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
  44. * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
  45. */
  46. public function refreshUrlsAndDirs(array $urls, array $dirs)
  47. {
  48. $req = array();
  49. if (!empty($urls)) {
  50. $req['urls'] = $urls;
  51. }
  52. if (!empty($dirs)) {
  53. $req['dirs'] = $dirs;
  54. }
  55. $url = $this->server . '/v2/tune/refresh';
  56. $body = json_encode($req);
  57. return $this->post($url, $body);
  58. }
  59. /**
  60. * 查询 CDN 刷新记录
  61. *
  62. * @param string $requestId 指定要查询记录所在的刷新请求id
  63. * @param string $isDir 指定是否查询目录,取值为 yes/no,默认不填则为两种类型记录都查询
  64. * @param array $urls 要查询的url列表,每个url可以是文件url,也可以是目录url
  65. * @param string $state 指定要查询记录的状态,取值processing/success/failure
  66. * @param int $pageNo 要求返回的页号,默认为0
  67. * @param int $pageSize 要求返回的页长度,默认为100
  68. * @param string $startTime 指定查询的开始日期,格式2006-01-01
  69. * @param string $endTime 指定查询的结束日期,格式2006-01-01
  70. * @return array
  71. * @link https://developer.qiniu.com/fusion/api/1229/cache-refresh#4
  72. */
  73. public function getCdnRefreshList(
  74. $requestId = null,
  75. $isDir = null,
  76. $urls = array(),
  77. $state = null,
  78. $pageNo = 0,
  79. $pageSize = 100,
  80. $startTime = null,
  81. $endTime = null
  82. ) {
  83. $req = array();
  84. \Qiniu\setWithoutEmpty($req, 'requestId', $requestId);
  85. \Qiniu\setWithoutEmpty($req, 'isDir', $isDir);
  86. \Qiniu\setWithoutEmpty($req, 'urls', $urls);
  87. \Qiniu\setWithoutEmpty($req, 'state', $state);
  88. \Qiniu\setWithoutEmpty($req, 'pageNo', $pageNo);
  89. \Qiniu\setWithoutEmpty($req, 'pageSize', $pageSize);
  90. \Qiniu\setWithoutEmpty($req, 'startTime', $startTime);
  91. \Qiniu\setWithoutEmpty($req, 'endTime', $endTime);
  92. $body = json_encode($req);
  93. $url = $this->server . '/v2/tune/refresh/list';
  94. return $this->post($url, $body);
  95. }
  96. /**
  97. * @param array $urls 待预取的文件链接数组
  98. *
  99. * @return array 预取的请求回复和错误,参考 examples/cdn_manager.php 代码
  100. *
  101. * @link http://developer.qiniu.com/article/fusion/api/refresh.html
  102. */
  103. public function prefetchUrls(array $urls)
  104. {
  105. $req = array(
  106. 'urls' => $urls,
  107. );
  108. $url = $this->server . '/v2/tune/prefetch';
  109. $body = json_encode($req);
  110. return $this->post($url, $body);
  111. }
  112. /**
  113. * 查询 CDN 预取记录
  114. *
  115. * @param string $requestId 指定要查询记录所在的刷新请求id
  116. * @param array $urls 要查询的url列表,每个url可以是文件url,也可以是目录url
  117. * @param string $state 指定要查询记录的状态,取值processing/success/failure
  118. * @param int $pageNo 要求返回的页号,默认为0
  119. * @param int $pageSize 要求返回的页长度,默认为100
  120. * @param string $startTime 指定查询的开始日期,格式2006-01-01
  121. * @param string $endTime 指定查询的结束日期,格式2006-01-01
  122. * @return array
  123. * @link https://developer.qiniu.com/fusion/api/1227/file-prefetching#4
  124. */
  125. public function getCdnPrefetchList(
  126. $requestId = null,
  127. $urls = array(),
  128. $state = null,
  129. $pageNo = 0,
  130. $pageSize = 100,
  131. $startTime = null,
  132. $endTime = null
  133. ) {
  134. $req = array();
  135. \Qiniu\setWithoutEmpty($req, 'requestId', $requestId);
  136. \Qiniu\setWithoutEmpty($req, 'urls', $urls);
  137. \Qiniu\setWithoutEmpty($req, 'state', $state);
  138. \Qiniu\setWithoutEmpty($req, 'pageNo', $pageNo);
  139. \Qiniu\setWithoutEmpty($req, 'pageSize', $pageSize);
  140. \Qiniu\setWithoutEmpty($req, 'startTime', $startTime);
  141. \Qiniu\setWithoutEmpty($req, 'endTime', $endTime);
  142. $body = json_encode($req);
  143. $url = $this->server . '/v2/tune/prefetch/list';
  144. return $this->post($url, $body);
  145. }
  146. /**
  147. * @param array $domains 待获取带宽数据的域名数组
  148. * @param string $startDate 开始的日期,格式类似 2017-01-01
  149. * @param string $endDate 结束的日期,格式类似 2017-01-01
  150. * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
  151. *
  152. * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
  153. *
  154. * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
  155. */
  156. public function getBandwidthData(array $domains, $startDate, $endDate, $granularity)
  157. {
  158. $req = array();
  159. $req['domains'] = implode(';', $domains);
  160. $req['startDate'] = $startDate;
  161. $req['endDate'] = $endDate;
  162. $req['granularity'] = $granularity;
  163. $url = $this->server . '/v2/tune/bandwidth';
  164. $body = json_encode($req);
  165. return $this->post($url, $body);
  166. }
  167. /**
  168. * @param array $domains 待获取流量数据的域名数组
  169. * @param string $startDate 开始的日期,格式类似 2017-01-01
  170. * @param string $endDate 结束的日期,格式类似 2017-01-01
  171. * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
  172. *
  173. * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
  174. *
  175. * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
  176. */
  177. public function getFluxData(array $domains, $startDate, $endDate, $granularity)
  178. {
  179. $req = array();
  180. $req['domains'] = implode(';', $domains);
  181. $req['startDate'] = $startDate;
  182. $req['endDate'] = $endDate;
  183. $req['granularity'] = $granularity;
  184. $url = $this->server . '/v2/tune/flux';
  185. $body = json_encode($req);
  186. return $this->post($url, $body);
  187. }
  188. /**
  189. * @param array $domains 待获取日志下载链接的域名数组
  190. * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
  191. *
  192. * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
  193. *
  194. * @link http://developer.qiniu.com/article/fusion/api/log.html
  195. */
  196. public function getCdnLogList(array $domains, $logDate)
  197. {
  198. $req = array();
  199. $req['domains'] = implode(';', $domains);
  200. $req['day'] = $logDate;
  201. $url = $this->server . '/v2/tune/log/list';
  202. $body = json_encode($req);
  203. return $this->post($url, $body);
  204. }
  205. private function post($url, $body)
  206. {
  207. $headers = $this->auth->authorization($url, $body, 'application/json');
  208. $headers['Content-Type'] = 'application/json';
  209. $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
  210. if (!$ret->ok()) {
  211. return array(null, new Error($url, $ret));
  212. }
  213. $r = ($ret->body === null) ? array() : $ret->json();
  214. return array($r, null);
  215. }
  216. /**
  217. * 构建时间戳防盗链鉴权的访问外链
  218. *
  219. * @param string $rawUrl 需要签名的资源url
  220. * @param string $encryptKey 时间戳防盗链密钥
  221. * @param string $durationInSeconds 链接的有效期(以秒为单位)
  222. *
  223. * @return string 带鉴权信息的资源外链,参考 examples/cdn_timestamp_antileech.php 代码
  224. */
  225. public static function createTimestampAntiLeechUrl($rawUrl, $encryptKey, $durationInSeconds)
  226. {
  227. $parsedUrl = parse_url($rawUrl);
  228. $deadline = time() + $durationInSeconds;
  229. $expireHex = dechex($deadline);
  230. $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
  231. $strToSign = $encryptKey . $path . $expireHex;
  232. $signStr = md5($strToSign);
  233. if (isset($parsedUrl['query'])) {
  234. $signedUrl = $rawUrl . '&sign=' . $signStr . '&t=' . $expireHex;
  235. } else {
  236. $signedUrl = $rawUrl . '?sign=' . $signStr . '&t=' . $expireHex;
  237. }
  238. return $signedUrl;
  239. }
  240. }