HttpBase.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @author wangkuiwei
  4. * @name HttpBase
  5. *
  6. */
  7. namespace App\Libs\Push\XMPush;
  8. class HttpBase {
  9. private $appSecret;
  10. private $region;
  11. private $isVip;
  12. /**
  13. * @param string $appSecret
  14. */
  15. public function setAppSecret($appSecret) {
  16. $this->appSecret = $appSecret;
  17. }
  18. /**
  19. * @param int $region
  20. */
  21. public function setRegion($region) {
  22. $this->region = $region;
  23. }
  24. /**
  25. * @param bool $isVip
  26. */
  27. public function setIsVip($isVip) {
  28. $this->isVip = $isVip;
  29. }
  30. public function __construct() {
  31. $this->appSecret = Constants::$secret;
  32. $this->region = Region::China;
  33. $this->isVip = false;
  34. }
  35. //发送请求,获取result,带重试
  36. public function getResult($requestPath, $fields, $retries) {
  37. $result = new Result($this->getReq($requestPath, $fields));
  38. if ($result->getErrorCode() == ErrorCode::Success) {
  39. return $result;
  40. }
  41. //重试
  42. for ($i = 0; $i < $retries; $i++) {
  43. $result = new Result($this->getReq($requestPath, $fields));
  44. if ($result->getErrorCode() == ErrorCode::Success) {
  45. break;
  46. }
  47. }
  48. return $result;
  49. }
  50. //get方式发送请求
  51. public function getReq($requestPath, $fields, $timeout = 3) {
  52. return $this->httpRequest($requestPath, $fields, "Get", $timeout);
  53. }
  54. //发送请求,获取result,带重试
  55. public function postResult($requestPath, $fields, $retries) {
  56. $result = new Result($this->postReq($requestPath, $fields));
  57. if ($result->getErrorCode() == ErrorCode::Success) {
  58. return $result;
  59. }
  60. //重试
  61. for ($i = 0; $i < $retries; $i++) {
  62. $result = new Result($this->postReq($requestPath, $fields));
  63. if ($result->getErrorCode() == ErrorCode::Success) {
  64. break;
  65. }
  66. }
  67. return $result;
  68. }
  69. //post方式发送请求
  70. public function postReq($requestPath, $fields, $timeout = 10) {
  71. return $this->httpRequest($requestPath, $fields, "Post", $timeout);
  72. }
  73. private function buildFullRequestURL(Server $server, PushRequestPath $requestPath) {
  74. return Constants::$HTTP_PROTOCOL . "://" . $server->getHost() . $requestPath->getPath();
  75. }
  76. private function httpRequest($requestPath, $fields, $method, $timeout = 10) {
  77. $server = ServerSwitch::getInstance()->selectServer($requestPath, $this->region, $this->isVip);
  78. $url = $this->buildFullRequestURL($server, $requestPath);
  79. $headers = array('Authorization: key=' . $this->appSecret,
  80. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  81. Constants::X_PUSH_SDK_VERSION . ': ' . Constants::SDK_VERSION);
  82. if (Constants::$autoSwitchHost && ServerSwitch::getInstance()->needRefreshHostList()) {
  83. array_push($headers, Constants::X_PUSH_HOST_LIST . ': true');
  84. }
  85. array_push($headers, "Expect:");
  86. // Open connection
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  89. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  92. curl_setopt($ch, CURLOPT_HEADER, true);
  93. if ($method == "Post") {
  94. curl_setopt($ch, CURLOPT_URL, $url);
  95. curl_setopt($ch, CURLOPT_POST, true);
  96. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  97. } else {
  98. curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($fields));
  99. curl_setopt($ch, CURLOPT_POST, false);
  100. }
  101. $content = curl_exec($ch);
  102. $result = "";
  103. if ($content !== false) {
  104. $info = curl_getinfo($ch);
  105. $total_time = $info['total_time'];
  106. if ($total_time > Constants::HOST_RESPONSE_EXPECT_TIME) {
  107. $server->decrPriority();
  108. } else {
  109. $server->incrPriority();
  110. }
  111. list($responseHeaderStr, $result) = explode("\r\n\r\n", $content, 2);
  112. $responseHeaders = $this->convertHeaders($responseHeaderStr);
  113. if (array_key_exists(Constants::X_PUSH_HOST_LIST, $responseHeaders)) {
  114. $serverListStr = $responseHeaders[Constants::X_PUSH_HOST_LIST];
  115. ServerSwitch::getInstance()->initialize($serverListStr);
  116. }
  117. } else {
  118. $server->decrPriority();
  119. $result = json_encode(array(
  120. "code" => ErrorCode::NETWORK_ERROR_TIMEOUT,
  121. "reason" => "network error or timeout"
  122. ));
  123. }
  124. // Close connection
  125. curl_close($ch);
  126. return $result;
  127. }
  128. /**
  129. * @param $responseHeaderStr
  130. * @return array
  131. */
  132. private function convertHeaders($responseHeaderStr) {
  133. $responseHeaderArr = explode("\r\n", $responseHeaderStr);
  134. $responseHeaders = array();
  135. foreach ($responseHeaderArr as $responseHeader) {
  136. $items = explode(":", $responseHeader, 2);
  137. if ($items !== false) {
  138. if (count($items) == 2) {
  139. $responseHeaders[trim($items[0])] = trim($items[1]);
  140. } else {
  141. $responseHeaders["Header_" . count($responseHeaders)] = trim($responseHeader);
  142. }
  143. }
  144. }
  145. return $responseHeaders;
  146. }
  147. }
  148. ?>