You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
5.3 KiB

  1. <?php
  2. namespace JPush;
  3. use JPush\Exceptions\APIConnectionException;
  4. use JPush\Exceptions\APIRequestException;
  5. final class Http {
  6. public static function get($client, $url) {
  7. $response = self::sendRequest($client, $url, Config::HTTP_GET, $body=null);
  8. return self::processResp($response);
  9. }
  10. public static function post($client, $url, $body) {
  11. $response = self::sendRequest($client, $url, Config::HTTP_POST, $body);
  12. return self::processResp($response);
  13. }
  14. public static function put($client, $url, $body) {
  15. $response = self::sendRequest($client, $url, Config::HTTP_PUT, $body);
  16. return self::processResp($response);
  17. }
  18. public static function delete($client, $url) {
  19. $response = self::sendRequest($client, $url, Config::HTTP_DELETE, $body=null);
  20. return self::processResp($response);
  21. }
  22. private static function sendRequest($client, $url, $method, $body=null, $times=1) {
  23. self::log($client, "Send " . $method . " " . $url . ", body:" . $body . ", times:" . $times);
  24. if (!defined('CURL_HTTP_VERSION_2_0')) {
  25. define('CURL_HTTP_VERSION_2_0', 3);
  26. }
  27. $ch = curl_init();
  28. curl_setopt($ch, CURLOPT_URL, $url);
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  30. curl_setopt($ch, CURLOPT_HEADER, true);
  31. curl_setopt($ch, CURLOPT_USERAGENT, Config::USER_AGENT);
  32. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Config::CONNECT_TIMEOUT); // 连接建立最长耗时
  33. curl_setopt($ch, CURLOPT_TIMEOUT, Config::READ_TIMEOUT); // 请求最长耗时
  34. // 设置SSL版本 1=CURL_SSLVERSION_TLSv1, 不指定使用默认值,curl会自动获取需要使用的CURL版本
  35. // curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37. // 如果报证书相关失败,可以考虑取消注释掉该行,强制指定证书版本
  38. //curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
  39. // 设置Basic认证
  40. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  41. curl_setopt($ch, CURLOPT_USERPWD, $client->getAuthStr());
  42. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  43. // 设置Post参数
  44. if ($method === Config::HTTP_POST) {
  45. curl_setopt($ch, CURLOPT_POST, true);
  46. } else if ($method === Config::HTTP_DELETE || $method === Config::HTTP_PUT) {
  47. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  48. }
  49. if (!is_null($body)) {
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  51. }
  52. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  53. 'Content-Type: application/json',
  54. 'Connection: Keep-Alive'
  55. ));
  56. $output = curl_exec($ch);
  57. $response = array();
  58. $errorCode = curl_errno($ch);
  59. if ($errorCode) {
  60. $retries = $client->getRetryTimes();
  61. if ($times < $retries) {
  62. return self::sendRequest($client, $url, $method, $body, ++$times);
  63. } else {
  64. if ($errorCode === 28) {
  65. throw new APIConnectionException("Response timeout. Your request has probably be received by JPush Server,please check that whether need to be pushed again.");
  66. } elseif ($errorCode === 56) {
  67. // resolve error[56 Problem (2) in the Chunked-Encoded data]
  68. throw new APIConnectionException("Response timeout, maybe cause by old CURL version. Your request has probably be received by JPush Server, please check that whether need to be pushed again.");
  69. } else {
  70. throw new APIConnectionException("Connect timeout. Please retry later. Error:" . $errorCode . " " . curl_error($ch));
  71. }
  72. }
  73. } else {
  74. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  75. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  76. $header_text = substr($output, 0, $header_size);
  77. $body = substr($output, $header_size);
  78. $headers = array();
  79. foreach (explode("\r\n", $header_text) as $i => $line) {
  80. if (!empty($line)) {
  81. if ($i === 0) {
  82. $headers[0] = $line;
  83. } else if (strpos($line, ": ")) {
  84. list ($key, $value) = explode(': ', $line);
  85. $headers[$key] = $value;
  86. }
  87. }
  88. }
  89. $response['headers'] = $headers;
  90. $response['body'] = $body;
  91. $response['http_code'] = $httpCode;
  92. }
  93. curl_close($ch);
  94. return $response;
  95. }
  96. public static function processResp($response) {
  97. if($response['http_code'] === 200) {
  98. $result = array();
  99. $data = json_decode($response['body'], true);
  100. if (!is_null($data)) {
  101. $result['body'] = $data;
  102. }
  103. $result['http_code'] = $response['http_code'];
  104. $result['headers'] = $response['headers'];
  105. return $result;
  106. } else {
  107. throw new APIRequestException($response);
  108. }
  109. }
  110. public static function log($client, $content) {
  111. if (!is_null($client->getLogFile())) {
  112. error_log($content . "\r\n", 3, $client->getLogFile());
  113. }
  114. }
  115. }