Web.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]  |
  4. // +---------------------------------------------------------------------+
  5. // | Copy right 2019-2029 www.niucloud.com  |
  6. // +---------------------------------------------------------------------+
  7. // | Author | NiuCloud <niucloud@outlook.com>  |
  8. // +---------------------------------------------------------------------+
  9. // | Repository | https://github.com/niucloud/framework.git  |
  10. // +---------------------------------------------------------------------+
  11. namespace app\model\system;
  12. use app\model\BaseModel;
  13. use think\facade\Cache;
  14. class Web extends BaseModel
  15. {
  16. private $url = "https://www.niushop.com.cn/api/%s";
  17. public function __construct()
  18. {
  19. }
  20. /**
  21. * 官网资讯
  22. */
  23. public function news()
  24. {
  25. $cache = Cache::get("new_day");
  26. if(!empty($cache)){
  27. return $cache;
  28. }
  29. $url = sprintf($this->url, 'news/news');
  30. $post_data = [
  31. ];
  32. $result = $this->doPost($url, $post_data);
  33. $res = json_decode($result, true);
  34. if($res["code"] >= 0){
  35. Cache::set("new_day", $res, 86400);
  36. }
  37. return $res;
  38. }
  39. /**
  40. * post 服务器请求
  41. */
  42. private function doPost($post_url, $post_data)
  43. {
  44. $ch = curl_init();
  45. curl_setopt($ch, CURLOPT_URL, $post_url);
  46. curl_setopt($ch, CURLOPT_HEADER, 0);
  47. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  49. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  50. if ($post_data != '' && !empty($post_data)) {
  51. curl_setopt($ch, CURLOPT_POST, 1);
  52. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  53. }
  54. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  55. $result = curl_exec($ch);
  56. curl_close($ch);
  57. return $result;
  58. }
  59. }