HttpHelper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  21. * @deprecated See: https://github.com/aliyun/openapi-sdk-php
  22. * Class HttpHelper
  23. */
  24. class HttpHelper
  25. {
  26. /**
  27. * @var int
  28. */
  29. public static $connectTimeout = 30;//30 second
  30. /**
  31. * @var int
  32. */
  33. public static $readTimeout = 80;//80 second
  34. /**
  35. * @param string $url
  36. * @param string $httpMethod
  37. * @param null $postFields
  38. * @param null $headers
  39. *
  40. * @return HttpResponse
  41. * @throws ClientException
  42. */
  43. public static function curl($url, $httpMethod = 'GET', $postFields = null, $headers = null)
  44. {
  45. $ch = curl_init();
  46. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
  47. if (ENABLE_HTTP_PROXY) {
  48. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  49. curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
  50. curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
  51. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  52. }
  53. curl_setopt($ch, CURLOPT_URL, $url);
  54. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields);
  57. if (self::$readTimeout) {
  58. curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
  59. }
  60. if (self::$connectTimeout) {
  61. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  62. }
  63. //https request
  64. if (strlen($url) > 5 && stripos($url, 'https') === 0) {
  65. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  67. }
  68. if (is_array($headers) && 0 < count($headers)) {
  69. $httpHeaders = self::getHttpHearders($headers);
  70. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  71. }
  72. $httpResponse = new HttpResponse();
  73. $httpResponse->setBody(curl_exec($ch));
  74. $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  75. if (curl_errno($ch)) {
  76. throw new ClientException('Server unreachable: Errno: ' . curl_errno($ch) . ' ' . curl_error($ch),
  77. 'SDK.ServerUnreachable');
  78. }
  79. curl_close($ch);
  80. return $httpResponse;
  81. }
  82. /**
  83. * @param $postFildes
  84. *
  85. * @return bool|string
  86. */
  87. public static function getPostHttpBody($postFildes)
  88. {
  89. $content = '';
  90. foreach ($postFildes as $apiParamKey => $apiParamValue) {
  91. $content .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  92. }
  93. return substr($content, 0, -1);
  94. }
  95. /**
  96. * @param $headers
  97. *
  98. * @return array
  99. */
  100. public static function getHttpHearders($headers)
  101. {
  102. $httpHeader = array();
  103. foreach ($headers as $key => $value) {
  104. $httpHeader[] = $key . ':' . $value;
  105. }
  106. return $httpHeader;
  107. }
  108. }