Curl.php 665 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Components;
  3. class Curl {
  4. /**
  5. * @param string $url 请求地址
  6. * @param array $data 数据,如果有数据则用POST请求
  7. *
  8. * @return mixed
  9. */
  10. public static function send($url, $data = []) {
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  13. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  14. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  16. curl_setopt($ch, CURLOPT_URL, $url);
  17. if($data){
  18. curl_setopt($ch, CURLOPT_POST, 1);
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  20. }
  21. $result = curl_exec($ch);
  22. curl_close($ch);
  23. return $result;
  24. }
  25. }