HttpHelper.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. namespace Mrgoon\AliyunSmsSdk\Http;
  21. class HttpHelper
  22. {
  23. public static $connectTimeout = 30;//30 second
  24. public static $readTimeout = 80;//80 second
  25. public static function curl($url, $httpMethod = "GET", $postFields = null,$headers = null)
  26. {
  27. $ch = curl_init();
  28. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
  29. if(ENABLE_HTTP_PROXY) {
  30. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
  31. curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
  32. curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
  33. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  34. }
  35. curl_setopt($ch, CURLOPT_URL, $url);
  36. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields);
  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("Server unreachable: Errno: " . curl_errno($ch) . " " . curl_error($ch), "SDK.ServerUnreachable");
  61. }
  62. curl_close($ch);
  63. return $httpResponse;
  64. }
  65. static function getPostHttpBody($postFildes){
  66. $content = "";
  67. foreach ($postFildes as $apiParamKey => $apiParamValue)
  68. {
  69. $content .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  70. }
  71. return substr($content, 0, -1);
  72. }
  73. static function getHttpHearders($headers)
  74. {
  75. $httpHeader = array();
  76. foreach ($headers as $key => $value)
  77. {
  78. array_push($httpHeader, $key.":".$value);
  79. }
  80. return $httpHeader;
  81. }
  82. }