common.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * curl 请求
  4. * @param $url string 请求地址
  5. * @param $headers json 请求头
  6. * @param $body json 请求体
  7. * @return mixed
  8. */
  9. function curlRequest($url, $headers = [], $body = [], $method = "GET")
  10. {
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置请求头
  14. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//设置请求体
  15. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  19. curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  20. $output = curl_exec($ch);
  21. curl_close($ch);
  22. $arr = json_decode($output, true);
  23. return $arr;
  24. }
  25. function http_curl($url, $type = 'get', $res = 'json', $arr = '')
  26. {
  27. //1.初始化curl
  28. $ch = curl_init();
  29. //2.设置curl的参数
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
  32. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. if ($type == 'post') {
  35. curl_setopt($ch, CURLOPT_POST, 1);
  36. curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
  37. }
  38. //3.采集
  39. $output = curl_exec($ch);
  40. //4.关闭
  41. curl_close($ch);
  42. if ($res == 'json') {
  43. return json_decode($output, true);
  44. }
  45. return $output;
  46. }