HttpHelper.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public static $connectTimeout = 30;//30 second
  23. public static $readTimeout = 80;//80 second
  24. public static function curl($url, $httpMethod = "GET", $postFields = null,$headers = null)
  25. {
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
  28. if(ENABLE_HTTP_PROXY) {
  29. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  30. curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
  31. curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
  32. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  33. }
  34. curl_setopt($ch, CURLOPT_URL, $url);
  35. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields);
  38. if (self::$readTimeout) {
  39. curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
  40. }
  41. if (self::$connectTimeout) {
  42. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  43. }
  44. //https request
  45. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  48. }
  49. if (is_array($headers) && 0 < count($headers))
  50. {
  51. $httpHeaders =self::getHttpHearders($headers);
  52. curl_setopt($ch,CURLOPT_HTTPHEADER,$httpHeaders);
  53. }
  54. $httpResponse = new HttpResponse();
  55. $httpResponse->setBody(curl_exec($ch));
  56. $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  57. if (curl_errno($ch))
  58. {
  59. throw new ClientException("Server unreachable: Errno: " . curl_errno($ch) . " " . curl_error($ch), "SDK.ServerUnreachable");
  60. }
  61. curl_close($ch);
  62. return $httpResponse;
  63. }
  64. static function getPostHttpBody($postFildes){
  65. $content = "";
  66. foreach ($postFildes as $apiParamKey => $apiParamValue)
  67. {
  68. $content .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  69. }
  70. return substr($content, 0, -1);
  71. }
  72. static function getHttpHearders($headers)
  73. {
  74. $httpHeader = array();
  75. foreach ($headers as $key => $value)
  76. {
  77. array_push($httpHeader, $key.":".$value);
  78. }
  79. return $httpHeader;
  80. }
  81. }