HttpHelper.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace common\services\mall\aliyunsts\StsCode\Http;
  3. use common\services\mall\aliyunsts\StsCode\Exception\ClientException;
  4. /*
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. */
  22. class HttpHelper
  23. {
  24. public static $connectTimeout = 30000;//30 second
  25. public static $readTimeout = 80000;//80 second
  26. public static function curl($url, $httpMethod = "GET", $postFields = null,$headers = null)
  27. {
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
  30. // if(ENABLE_HTTP_PROXY) {
  31. // curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  32. // curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
  33. // curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
  34. // curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  35. // }
  36. curl_setopt($ch, CURLOPT_URL, $url);
  37. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  39. if (self::$readTimeout) {
  40. curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
  41. }
  42. if (self::$connectTimeout) {
  43. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  44. }
  45. //https request
  46. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  49. }
  50. if (is_array($headers) && 0 < count($headers))
  51. {
  52. $httpHeaders =self::getHttpHearders($headers);
  53. curl_setopt($ch,CURLOPT_HTTPHEADER,$httpHeaders);
  54. }
  55. $httpResponse = new HttpResponse();
  56. $httpResponse->setBody(curl_exec($ch));
  57. $httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  58. if (curl_errno($ch))
  59. {
  60. throw new ClientException("Speicified endpoint or uri is not valid.", "SDK.ServerUnreachable");
  61. }
  62. curl_close($ch);
  63. return $httpResponse;
  64. }
  65. static function getHttpHearders($headers)
  66. {
  67. $httpHeader = array();
  68. foreach ($headers as $key => $value)
  69. {
  70. array_push($httpHeader, $key.":".$value);
  71. }
  72. return $httpHeader;
  73. }
  74. }