Zone.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Qiniu;
  3. use Qiniu\Http\Client;
  4. use Qiniu\Http\Error;
  5. final class Zone
  6. {
  7. //源站上传域名
  8. public $srcUpHosts;
  9. //CDN加速上传域名
  10. public $cdnUpHosts;
  11. //资源管理域名
  12. public $rsHost;
  13. //资源列举域名
  14. public $rsfHost;
  15. //资源处理域名
  16. public $apiHost;
  17. //IOVIP域名
  18. public $iovipHost;
  19. //构造一个Zone对象
  20. public function __construct(
  21. $srcUpHosts = array(),
  22. $cdnUpHosts = array(),
  23. $rsHost = "rs.qiniu.com",
  24. $rsfHost = "rsf.qiniu.com",
  25. $apiHost = "api.qiniu.com",
  26. $iovipHost = null
  27. ) {
  28. $this->srcUpHosts = $srcUpHosts;
  29. $this->cdnUpHosts = $cdnUpHosts;
  30. $this->rsHost = $rsHost;
  31. $this->rsfHost = $rsfHost;
  32. $this->apiHost = $apiHost;
  33. $this->iovipHost = $iovipHost;
  34. }
  35. //华东机房
  36. public static function zone0()
  37. {
  38. $Zone_z0 = new Zone(
  39. array("up.qiniup.com", 'up-jjh.qiniup.com', 'up-xs.qiniup.com'),
  40. array('upload.qiniup.com', 'upload-jjh.qiniup.com', 'upload-xs.qiniup.com'),
  41. 'rs.qiniu.com',
  42. 'rsf.qiniu.com',
  43. 'api.qiniu.com',
  44. 'iovip.qbox.me'
  45. );
  46. return $Zone_z0;
  47. }
  48. //华北机房
  49. public static function zone1()
  50. {
  51. $Zone_z1 = new Zone(
  52. array('up-z1.qiniup.com'),
  53. array('upload-z1.qiniup.com'),
  54. "rs-z1.qiniu.com",
  55. "rsf-z1.qiniu.com",
  56. "api-z1.qiniu.com",
  57. "iovip-z1.qbox.me"
  58. );
  59. return $Zone_z1;
  60. }
  61. //华南机房
  62. public static function zone2()
  63. {
  64. $Zone_z2 = new Zone(
  65. array('up-z2.qiniup.com', 'up-dg.qiniup.com', 'up-fs.qiniup.com'),
  66. array('upload-z2.qiniup.com', 'upload-dg.qiniup.com', 'upload-fs.qiniup.com'),
  67. "rs-z2.qiniu.com",
  68. "rsf-z2.qiniu.com",
  69. "api-z2.qiniu.com",
  70. "iovip-z2.qbox.me"
  71. );
  72. return $Zone_z2;
  73. }
  74. //北美机房
  75. public static function zoneNa0()
  76. {
  77. //北美机房
  78. $Zone_na0 = new Zone(
  79. array('up-na0.qiniup.com'),
  80. array('upload-na0.qiniup.com'),
  81. "rs-na0.qiniu.com",
  82. "rsf-na0.qiniu.com",
  83. "api-na0.qiniu.com",
  84. "iovip-na0.qbox.me"
  85. );
  86. return $Zone_na0;
  87. }
  88. //新加坡机房
  89. public static function zoneAs0()
  90. {
  91. //新加坡机房
  92. $Zone_as0 = new Zone(
  93. array('up-as0.qiniup.com'),
  94. array('upload-as0.qiniup.com'),
  95. "rs-as0.qiniu.com",
  96. "rsf-as0.qiniu.com",
  97. "api-as0.qiniu.com",
  98. "iovip-as0.qbox.me"
  99. );
  100. return $Zone_as0;
  101. }
  102. /*
  103. * GET /v2/query?ak=<ak>&&bucket=<bucket>
  104. **/
  105. public static function queryZone($ak, $bucket)
  106. {
  107. $zone = new Zone();
  108. $url = Config::UC_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
  109. $ret = Client::Get($url);
  110. if (!$ret->ok()) {
  111. return array(null, new Error($url, $ret));
  112. }
  113. $r = ($ret->body === null) ? array() : $ret->json();
  114. //print_r($ret);
  115. //parse zone;
  116. $iovipHost = $r['io']['src']['main'][0];
  117. $zone->iovipHost = $iovipHost;
  118. $accMain = $r['up']['acc']['main'][0];
  119. array_push($zone->cdnUpHosts, $accMain);
  120. if (isset($r['up']['acc']['backup'])) {
  121. foreach ($r['up']['acc']['backup'] as $key => $value) {
  122. array_push($zone->cdnUpHosts, $value);
  123. }
  124. }
  125. $srcMain = $r['up']['src']['main'][0];
  126. array_push($zone->srcUpHosts, $srcMain);
  127. if (isset($r['up']['src']['backup'])) {
  128. foreach ($r['up']['src']['backup'] as $key => $value) {
  129. array_push($zone->srcUpHosts, $value);
  130. }
  131. }
  132. //set specific hosts
  133. if (strstr($zone->iovipHost, "z1") !== false) {
  134. $zone->rsHost = "rs-z1.qiniu.com";
  135. $zone->rsfHost = "rsf-z1.qiniu.com";
  136. $zone->apiHost = "api-z1.qiniu.com";
  137. } elseif (strstr($zone->iovipHost, "z2") !== false) {
  138. $zone->rsHost = "rs-z2.qiniu.com";
  139. $zone->rsfHost = "rsf-z2.qiniu.com";
  140. $zone->apiHost = "api-z2.qiniu.com";
  141. } elseif (strstr($zone->iovipHost, "na0") !== false) {
  142. $zone->rsHost = "rs-na0.qiniu.com";
  143. $zone->rsfHost = "rsf-na0.qiniu.com";
  144. $zone->apiHost = "api-na0.qiniu.com";
  145. } elseif (strstr($zone->iovipHost, "as0") !== false) {
  146. $zone->rsHost = "rs-as0.qiniu.com";
  147. $zone->rsfHost = "rsf-as0.qiniu.com";
  148. $zone->apiHost = "api-as0.qiniu.com";
  149. } else {
  150. $zone->rsHost = "rs.qiniu.com";
  151. $zone->rsfHost = "rsf.qiniu.com";
  152. $zone->apiHost = "api.qiniu.com";
  153. }
  154. return $zone;
  155. }
  156. }