HttpHelper.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 = 30000;//30 second
  23. public static $readTimeout = 80000;//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. if (self::$readTimeout) {
  38. curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
  39. }
  40. if (self::$connectTimeout) {
  41. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  42. }
  43. //https request
  44. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  45. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  46. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  47. }
  48. if (is_array($headers) && 0 < count($headers))
  49. {
  50. $httpHeaders =self::getHttpHearders($headers);
  51. curl_setopt($ch,CURLOPT_HTTPHEADER,$httpHeaders);
  52. }
  53. $httpResponse = new HttpResponse();
  54. $httpResponse->setBody(curl_exec($ch));
  55. $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  56. if (curl_errno($ch))
  57. {
  58. throw new ClientException("Speicified endpoint or uri is not valid.", "SDK.ServerUnreachable");
  59. }
  60. curl_close($ch);
  61. return $httpResponse;
  62. }
  63. static function getHttpHearders($headers)
  64. {
  65. $httpHeader = array();
  66. foreach ($headers as $key => $value)
  67. {
  68. array_push($httpHeader, $key.":".$value);
  69. }
  70. return $httpHeader;
  71. }
  72. }