HttpBase.php 4.9 KB

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