CdnManager.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Qiniu\Cdn;
  3. use Qiniu\Auth;
  4. use Qiniu\Http\Error;
  5. use Qiniu\Http\Client;
  6. final class CdnManager
  7. {
  8. private $auth;
  9. private $server;
  10. public function __construct(Auth $auth)
  11. {
  12. $this->auth = $auth;
  13. $this->server = 'http://fusion.qiniuapi.com';
  14. }
  15. /**
  16. * @param array $urls 待刷新的文件链接数组
  17. * @return array
  18. */
  19. public function refreshUrls(array $urls)
  20. {
  21. return $this->refreshUrlsAndDirs($urls, array());
  22. }
  23. /**
  24. * @param array $dirs 待刷新的文件链接数组
  25. * @return array
  26. * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
  27. * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
  28. */
  29. public function refreshDirs(array $dirs)
  30. {
  31. return $this->refreshUrlsAndDirs(array(), $dirs);
  32. }
  33. /**
  34. * @param array $urls 待刷新的文件链接数组
  35. * @param array $dirs 待刷新的目录链接数组
  36. *
  37. * @return array 刷新的请求回复和错误,参考 examples/cdn_manager.php 代码
  38. * @link http://developer.qiniu.com/article/fusion/api/refresh.html
  39. *
  40. * 目前客户默认没有目录刷新权限,刷新会有400038报错,参考:https://developer.qiniu.com/fusion/api/1229/cache-refresh
  41. * 需要刷新目录请工单联系技术支持 https://support.qiniu.com/tickets/category
  42. */
  43. public function refreshUrlsAndDirs(array $urls, array $dirs)
  44. {
  45. $req = array();
  46. if (!empty($urls)) {
  47. $req['urls'] = $urls;
  48. }
  49. if (!empty($dirs)) {
  50. $req['dirs'] = $dirs;
  51. }
  52. $url = $this->server . '/v2/tune/refresh';
  53. $body = json_encode($req);
  54. return $this->post($url, $body);
  55. }
  56. /**
  57. * @param array $urls 待预取的文件链接数组
  58. *
  59. * @return array 预取的请求回复和错误,参考 examples/cdn_manager.php 代码
  60. *
  61. * @link http://developer.qiniu.com/article/fusion/api/refresh.html
  62. */
  63. public function prefetchUrls(array $urls)
  64. {
  65. $req = array(
  66. 'urls' => $urls,
  67. );
  68. $url = $this->server . '/v2/tune/prefetch';
  69. $body = json_encode($req);
  70. return $this->post($url, $body);
  71. }
  72. /**
  73. * @param array $domains 待获取带宽数据的域名数组
  74. * @param string $startDate 开始的日期,格式类似 2017-01-01
  75. * @param string $endDate 结束的日期,格式类似 2017-01-01
  76. * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
  77. *
  78. * @return array 带宽数据和错误信息,参考 examples/cdn_manager.php 代码
  79. *
  80. * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
  81. */
  82. public function getBandwidthData(array $domains, $startDate, $endDate, $granularity)
  83. {
  84. $req = array();
  85. $req['domains'] = implode(';', $domains);
  86. $req['startDate'] = $startDate;
  87. $req['endDate'] = $endDate;
  88. $req['granularity'] = $granularity;
  89. $url = $this->server . '/v2/tune/bandwidth';
  90. $body = json_encode($req);
  91. return $this->post($url, $body);
  92. }
  93. /**
  94. * @param array $domains 待获取流量数据的域名数组
  95. * @param string $startDate 开始的日期,格式类似 2017-01-01
  96. * @param string $endDate 结束的日期,格式类似 2017-01-01
  97. * @param string $granularity 获取数据的时间间隔,可以是 5min, hour 或者 day
  98. *
  99. * @return array 流量数据和错误信息,参考 examples/cdn_manager.php 代码
  100. *
  101. * @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
  102. */
  103. public function getFluxData(array $domains, $startDate, $endDate, $granularity)
  104. {
  105. $req = array();
  106. $req['domains'] = implode(';', $domains);
  107. $req['startDate'] = $startDate;
  108. $req['endDate'] = $endDate;
  109. $req['granularity'] = $granularity;
  110. $url = $this->server . '/v2/tune/flux';
  111. $body = json_encode($req);
  112. return $this->post($url, $body);
  113. }
  114. /**
  115. * @param array $domains 待获取日志下载链接的域名数组
  116. * @param string $logDate 获取指定日期的日志下载链接,格式类似 2017-01-01
  117. *
  118. * @return array 日志下载链接数据和错误信息,参考 examples/cdn_manager.php 代码
  119. *
  120. * @link http://developer.qiniu.com/article/fusion/api/log.html
  121. */
  122. public function getCdnLogList(array $domains, $logDate)
  123. {
  124. $req = array();
  125. $req['domains'] = implode(';', $domains);
  126. $req['day'] = $logDate;
  127. $url = $this->server . '/v2/tune/log/list';
  128. $body = json_encode($req);
  129. return $this->post($url, $body);
  130. }
  131. private function post($url, $body)
  132. {
  133. $headers = $this->auth->authorization($url, $body, 'application/json');
  134. $headers['Content-Type'] = 'application/json';
  135. $ret = Client::post($url, $body, $headers);
  136. if (!$ret->ok()) {
  137. return array(null, new Error($url, $ret));
  138. }
  139. $r = ($ret->body === null) ? array() : $ret->json();
  140. return array($r, null);
  141. }
  142. /**
  143. * 构建时间戳防盗链鉴权的访问外链
  144. *
  145. * @param string $rawUrl 需要签名的资源url
  146. * @param string $encryptKey 时间戳防盗链密钥
  147. * @param string $durationInSeconds 链接的有效期(以秒为单位)
  148. *
  149. * @return string 带鉴权信息的资源外链,参考 examples/cdn_timestamp_antileech.php 代码
  150. */
  151. public static function createTimestampAntiLeechUrl($rawUrl, $encryptKey, $durationInSeconds)
  152. {
  153. $parsedUrl = parse_url($rawUrl);
  154. $deadline = time() + $durationInSeconds;
  155. $expireHex = dechex($deadline);
  156. $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
  157. $strToSign = $encryptKey . $path . $expireHex;
  158. $signStr = md5($strToSign);
  159. if (isset($parsedUrl['query'])) {
  160. $signedUrl = $rawUrl . '&sign=' . $signStr . '&t=' . $expireHex;
  161. } else {
  162. $signedUrl = $rawUrl . '?sign=' . $signStr . '&t=' . $expireHex;
  163. }
  164. return $signedUrl;
  165. }
  166. }