CurlUtil.php 3.2 KB

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