Common.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Http\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 代理管理模型类
  6. */
  7. class Common extends Model
  8. {
  9. /**
  10. * 通用批量查询
  11. *
  12. * @access public
  13. * @param mixed $database 数据库
  14. * @param mixed $where 查询条件
  15. * @param mixed $orWhere 或查询 [[['batch_id', '191'], ['batch_id', '186']],[['id', '55'], ['id', '59']]];
  16. * @param mixed $orderBy 排序字段 ['sort' => 'asc']
  17. * @return String
  18. */
  19. public function make_request($url, $params, $timeout = 30)
  20. {
  21. set_time_limit(0);
  22. if (function_exists('curl_init')) {
  23. // Use CURL if installed...
  24. $ch = curl_init();
  25. $header = array(
  26. 'Accept-Language: zh-cn',
  27. 'Connection: Keep-Alive',
  28. 'Cache-Control: no-cache'
  29. );
  30. curl_setopt($ch, CURLOPT_POST, 1);
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  35. if ($timeout > 0) curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  36. $result = curl_exec($ch);
  37. $errno = curl_errno($ch);
  38. curl_close($ch);
  39. return $result;
  40. } else {
  41. $context = array(
  42. 'http' => array(
  43. 'method' => 'POST',
  44. 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" .
  45. 'Content-length: ' . strlen($params),
  46. 'content' => $params));
  47. if ($timeout > 0) $context['http']['timeout'] = $timeout;
  48. $contextid = stream_context_create($context);
  49. $sock = @fopen($url, 'r', false, $contextid);
  50. if ($sock) {
  51. $result = '';
  52. while (!feof($sock)) {
  53. $result .= fgets($sock, 8192);
  54. }
  55. fclose($sock);
  56. } else {
  57. return 'TimeOut';
  58. }
  59. }
  60. return $result;
  61. }//end commonSelect()
  62. }