Grab.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\index\service;
  3. use think\Config;
  4. class Grab
  5. {
  6. private static $grab;
  7. protected $config =[];
  8. public $urls = [];
  9. public function __construct($config=[])
  10. {
  11. $defConfig = config();
  12. $this->config = isset($defConfig['grab'])? $defConfig['grab'] : [];
  13. $this->config = $config? $config : $this->config;
  14. $this->urls = isset($this->config['urls'])? $this->config['urls'] : [];
  15. }
  16. /**
  17. * 初始化
  18. * @param array $config
  19. * @return Grab
  20. */
  21. public static function instance($config=[]){
  22. if(!self::$grab){
  23. self::$grab = new Grab($config);
  24. }
  25. return self::$grab;
  26. }
  27. /**
  28. * 抓取数据
  29. * @param string $resource
  30. */
  31. public function grabData($resource=''){
  32. var_dump($this->urls);
  33. $urls = isset($this->urls[$resource])? $this->urls[$resource] : '';
  34. $url = isset($urls['url'])? trim($urls['url']) : '';
  35. echo $url."\n";
  36. if($url){
  37. $cookie = file_get_contents("./logs/{$resource}.cookie");
  38. $header = [
  39. "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
  40. "Host:www.nike.com",
  41. "Upgrade-Insecure-Requests:1",
  42. "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
  43. "Content-Type:text/html; charset=utf-8",
  44. // "Cookie:{$cookie}",
  45. ];
  46. // $content = file_get_contents($url);
  47. $content = grabRequest($url, $header,'s=upcoming');
  48. file_put_contents('./logs/'.$resource.'_'.date('YmdHis').'.html', $content);
  49. }
  50. }
  51. }