CurlUtil.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\utils;
  3. class CurlUtil{
  4. public static function getHttp($url,$ssl = true){
  5. if (empty($url)) {
  6. return '';
  7. } elseif (strrpos($url, 'http') !== false) {
  8. return $url;
  9. } else {
  10. $domain = $_SERVER['HTTP_HOST'];
  11. $http = 'http://';
  12. $ssl && $http = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  13. return $http.$domain.$url;
  14. }
  15. }
  16. /**
  17. * 模拟http get请求
  18. * @param $url
  19. * @param array $params
  20. * @param array $header
  21. * @param int $timeout
  22. * @return bool|string
  23. * @throws \Exception
  24. */
  25. public static function get($url, $params=array(), $header = array(),$timeout=30){
  26. $opts = array(
  27. CURLOPT_TIMEOUT => $timeout,
  28. CURLOPT_RETURNTRANSFER => 1,
  29. CURLOPT_SSL_VERIFYPEER => false,
  30. CURLOPT_SSL_VERIFYHOST => false,
  31. CURLOPT_HTTPHEADER => $header,
  32. CURLOPT_URL => count($params) > 0 ? $url . '?' . http_build_query($params) : $url,
  33. );
  34. $ch = curl_init();
  35. curl_setopt_array($ch, $opts);
  36. $data = curl_exec($ch);
  37. $error = curl_error($ch);
  38. curl_close($ch);
  39. if ($error)
  40. throw new \Exception('http get error:' . $error);
  41. return $data;
  42. }
  43. /**
  44. * 模拟http post请求
  45. * @param $url
  46. * @param $params
  47. * @param array $header
  48. * @param int $timeout
  49. * @return bool|string
  50. * @throws \Exception
  51. */
  52. public static function post($url, $params, $header = array(), $timeout=30){
  53. $opts = array(
  54. CURLOPT_TIMEOUT => $timeout,
  55. CURLOPT_RETURNTRANSFER => 1,
  56. CURLOPT_SSL_VERIFYPEER => false,
  57. CURLOPT_SSL_VERIFYHOST => false,
  58. CURLOPT_HTTPHEADER => $header,
  59. CURLOPT_URL => $url,
  60. CURLOPT_POST => 1,
  61. CURLOPT_POSTFIELDS => is_array($params) ? http_build_query($params) : $params
  62. );
  63. $ch = curl_init();
  64. curl_setopt_array($ch, $opts);
  65. $data = curl_exec($ch);
  66. $error = curl_error($ch);
  67. curl_close($ch);
  68. if ($error)
  69. throw new \Exception('http post error:' . $error);
  70. return $data;
  71. }
  72. /**
  73. * 模拟http post json请求
  74. * @param string $url
  75. * @param array $params
  76. * @param array $header
  77. * @param int $timeout
  78. * @return bool|string
  79. * @throws \Exception
  80. */
  81. public static function postJson(string $url, array $params, array $header = [], int $timeout=30){
  82. $opts = [
  83. CURLOPT_TIMEOUT => $timeout,
  84. CURLOPT_RETURNTRANSFER => 1,
  85. CURLOPT_SSL_VERIFYPEER => false,
  86. CURLOPT_SSL_VERIFYHOST => false,
  87. CURLOPT_HTTPHEADER => array_merge(["content-type:application/json"],$header),
  88. CURLOPT_URL => $url,
  89. CURLOPT_POST => 1,
  90. CURLOPT_POSTFIELDS => json_encode($params)
  91. ];
  92. $ch = curl_init();
  93. curl_setopt_array($ch, $opts);
  94. $data = curl_exec($ch);
  95. $error = curl_error($ch);
  96. curl_close($ch);
  97. if ($error)
  98. throw new \Exception('http post error:' . $error);
  99. return json_decode($data,true);
  100. }
  101. }