Service.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Service\Wx;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Redis;
  5. /**
  6. * 公用的公共服务
  7. * @package App\Models
  8. */
  9. class Service
  10. {
  11. public $log;
  12. public $total = 0;
  13. public function __construct()
  14. {
  15. }
  16. function curlOpen($url, $config = array())
  17. {
  18. $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 100, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>true,'isupfile'=>false,'returnheader'=>false,'request'=>'post');
  19. $arr = array_merge($arr, $config);
  20. $ch = curl_init();
  21. curl_setopt($ch, CURLOPT_URL, $url);
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
  23. curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
  24. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  25. curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
  26. curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
  27. curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
  28. curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);//��ȡheader
  29. if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  30. if($arr['ssl'])
  31. {
  32. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  34. }
  35. if(!empty($arr['cookie']))
  36. {
  37. curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
  38. curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
  39. }
  40. if(!empty($arr['proxy']))
  41. {
  42. //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  43. curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
  44. if(!empty($arr['userpwd']))
  45. {
  46. curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
  47. }
  48. }
  49. //ip�Ƚ����⣬�ü�ֵ��ʾ
  50. if(!empty($arr['header']['ip']))
  51. {
  52. array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
  53. unset($arr['header']['ip']);
  54. }
  55. $arr['header'] = array_filter($arr['header']);
  56. if(!empty($arr['header']))
  57. {
  58. curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
  59. }
  60. if($arr['request'] === 'put'){
  61. curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  62. curl_setopt($ch, CURLOPT_POSTFIELDS,$arr['post']);
  63. }elseif($arr['post'] != false)
  64. {
  65. curl_setopt($ch, CURLOPT_POST, true);
  66. if(is_array($arr['post']) && $arr['isupfile'] === false)
  67. {
  68. $post = http_build_query($arr['post']);
  69. }
  70. else
  71. {
  72. $post = $arr['post'];
  73. }
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  75. }
  76. $result = curl_exec($ch);
  77. curl_close($ch);
  78. return $result;
  79. }
  80. //分页共用
  81. public function limit($db, $columns, $request)
  82. {
  83. $per_page = $request['page_size'] ?? 9999;
  84. $page = $request['page_index'] ?? 1;
  85. $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
  86. $data['total'] = $return['total'];
  87. $data['data'] = $return['data'];
  88. return $data;
  89. }
  90. //判断是否为空
  91. protected function isEmpty($data, $word)
  92. {
  93. if (isset($data[$word]) && (!empty($data[$word]) || $data[$word] === '0'|| $data[$word] === 0)) return false;
  94. return true;
  95. }
  96. //后台端 某些需要限制请求频率的接口
  97. public function limitingSendRequestBackg($key,$ttl = 5){
  98. if($ttl < 5) $ttl = 5;
  99. // 使用Redis Facade设置,当键名不存在时才设置成功
  100. if (Redis::setnx($key, 1)) {
  101. Redis::expire($key, $ttl); //多少秒后过期
  102. return [true, ''];
  103. }
  104. return [false,'操作频繁, 请在 ' . $ttl . '秒后重试'];
  105. }
  106. }