暫無描述

HttpBase.php 4.9KB

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