common.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. function curl(string $url, $data, $type = 'form') {
  13. switch ($type) {
  14. case 'json':
  15. $data = json_encode($data);
  16. $header = ['Content-Type: application/json', 'Content-Length:' . strlen($data)];
  17. break;
  18. case 'form':
  19. $data = http_build_query($data);
  20. $header = ['Content-Type: application/x-www-form-urlencoded'];
  21. }
  22. $ch = curl_init();
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. curl_setopt($ch, CURLOPT_POST, 1);
  25. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
  26. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  27. curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  29. $output = curl_exec($ch);
  30. curl_close($ch);
  31. return $output;
  32. }