functions.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. if(!function_exists("curl_request")) {
  3. /**
  4. * http请求
  5. * @param string $url 请求url
  6. * @param array $data 请求内容
  7. * @param string $method 请求类型
  8. * @param array $header 请求头
  9. * @param boolean $https 是否是ssl
  10. * @param int $timeout 超时时间
  11. * @return string
  12. */
  13. function curl_request($url, $data = null, $method = 'post', $header = array("content-type: application/json"), $https = true, $timeout = 5)
  14. {
  15. $method = strtoupper($method);
  16. $ch = curl_init();//初始化
  17. curl_setopt($ch, CURLOPT_URL, $url);//访问的URL
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容,但不输出
  19. curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
  20. if ($https) {
  21. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求 不验证证书
  22. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求 不验证HOST
  23. }
  24. if ($method != "GET") {
  25. if ($method == 'POST') {
  26. curl_setopt($ch, CURLOPT_POST, true);//请求方式为post请求
  27. }
  28. if ($method == 'PUT' || strtoupper($method) == 'DELETE') {
  29. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
  30. }
  31. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//请求数据
  32. }
  33. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  34. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
  35. //curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息
  36. $result = curl_exec($ch);//执行请求
  37. curl_close($ch);//关闭curl,释放资源
  38. return $result;
  39. }
  40. }