HttpHelper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class HttpHelper
  21. {
  22. /**
  23. * @var int
  24. */
  25. public static $connectTimeout = 30;//30 second
  26. /**
  27. * @var int
  28. */
  29. public static $readTimeout = 80;//80 second
  30. /**
  31. * @param string $url
  32. * @param string $httpMethod
  33. * @param null $postFields
  34. * @param null $headers
  35. *
  36. * @return HttpResponse
  37. * @throws ClientException
  38. */
  39. public static function curl($url, $httpMethod = 'GET', $postFields = null, $headers = null)
  40. {
  41. $ch = curl_init();
  42. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
  43. if (ENABLE_HTTP_PROXY) {
  44. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  45. curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
  46. curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
  47. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  48. }
  49. curl_setopt($ch, CURLOPT_URL, $url);
  50. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  52. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields);
  53. if (self::$readTimeout) {
  54. curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
  55. }
  56. if (self::$connectTimeout) {
  57. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  58. }
  59. //https request
  60. if (strlen($url) > 5 && stripos($url, 'https') === 0) {
  61. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  62. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  63. }
  64. if (is_array($headers) && 0 < count($headers)) {
  65. $httpHeaders = self::getHttpHearders($headers);
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  67. }
  68. $httpResponse = new HttpResponse();
  69. $httpResponse->setBody(curl_exec($ch));
  70. $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  71. if (curl_errno($ch)) {
  72. throw new ClientException('Server unreachable: Errno: ' . curl_errno($ch) . ' ' . curl_error($ch),
  73. 'SDK.ServerUnreachable');
  74. }
  75. curl_close($ch);
  76. return $httpResponse;
  77. }
  78. /**
  79. * @param $postFildes
  80. *
  81. * @return bool|string
  82. */
  83. public static function getPostHttpBody($postFildes)
  84. {
  85. $content = '';
  86. foreach ($postFildes as $apiParamKey => $apiParamValue) {
  87. $content .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  88. }
  89. return substr($content, 0, -1);
  90. }
  91. /**
  92. * @param $headers
  93. *
  94. * @return array
  95. */
  96. public static function getHttpHearders($headers)
  97. {
  98. $httpHeader = array();
  99. foreach ($headers as $key => $value) {
  100. $httpHeader[] = $key . ':' . $value;
  101. }
  102. return $httpHeader;
  103. }
  104. }