| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace app\index\service;
- use think\Config;
- class Grab
- {
- private static $grab;
- protected $config =[];
- public $urls = [];
- public function __construct($config=[])
- {
- $defConfig = config();
- $this->config = isset($defConfig['grab'])? $defConfig['grab'] : [];
- $this->config = $config? $config : $this->config;
- $this->urls = isset($this->config['urls'])? $this->config['urls'] : [];
- }
- /**
- * 初始化
- * @param array $config
- * @return Grab
- */
- public static function instance($config=[]){
- if(!self::$grab){
- self::$grab = new Grab($config);
- }
- return self::$grab;
- }
- /**
- * 抓取数据
- * @param string $resource
- */
- public function grabData($resource=''){
- var_dump($this->urls);
- $urls = isset($this->urls[$resource])? $this->urls[$resource] : '';
- $url = isset($urls['url'])? trim($urls['url']) : '';
- echo $url."\n";
- if($url){
- $cookie = file_get_contents("./logs/{$resource}.cookie");
- $header = [
- "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",
- "Host:www.nike.com",
- "Upgrade-Insecure-Requests:1",
- "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",
- "Content-Type:text/html; charset=utf-8",
- // "Cookie:{$cookie}",
- ];
- // $content = file_get_contents($url);
- $content = grabRequest($url, $header,'s=upcoming');
- file_put_contents('./logs/'.$resource.'_'.date('YmdHis').'.html', $content);
- }
- }
- }
|