| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- set_time_limit(0);
- require_once('vendor/autoload.php');
- use League\Flysystem\Plugin\ListFiles;
- use League\Flysystem\Plugin\ListPaths;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- error_reporting(0);
- $settings = [
- 'baseUri' => 'https://www.meitu.live:433/remote.php/webdav/',
- 'userName' => "wangyating",
- 'password' => "wyt4913402",
- 'authType'=>CURLAUTH_BASIC
- ];
- $client = new Sabre\DAV\Client($settings);
- $adapter = new League\Flysystem\WebDAV\WebDAVAdapter($client,"/remote.php/webdav");
- $flysystem = new League\Flysystem\Filesystem($adapter);
- $flysystem->addPlugin(new ListFiles());
- $flysystem->addPlugin(new ListPaths());
- $wd = $argv[1];
- $deep = $argv[2] ?: 1;
- $keys = explode(",", $wd);
- $result = $response = [];
- baidu($keys);
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setTitle("为你推荐-相关搜索词");
- $row = 1;
- $sheet->setCellValue("A" . $row, "序号");
- $sheet->setCellValue("B" . $row, "关键词");
- $sheet->setCellValue("C" . $row, "相关搜索词");
- $row++;
- foreach ($result as $key => $item) {
- $sheet->setCellValue("A" . $row, $row - 1);
- $sheet->setCellValue("B" . $row, $item['key']);
- $sheet->setCellValue("C" . $row, $item['relatedWd']);
- $row++;
- }
- $styleArray = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- ];
- $sheet->getColumnDimension("A")->setWidth(14);
- $sheet->getColumnDimension("B")->setWidth(25);
- $sheet->getColumnDimension("C")->setWidth(25);
- $sheet->getStyle("A1:C" . $row)->applyFromArray($styleArray);
- $writer = new Xlsx($spreadsheet);
- //$file_path = sprintf(sys_get_temp_dir() . "/%s-%s.xlsx", $wd, date("Y-m-d"));
- $file_path = tempnam(sys_get_temp_dir(), 'baidu');;
- $writer->save($file_path);
- $flysystem->put(sprintf("关键词/关键词拓展/百度/百度为您推荐/%s-%s.xlsx",$wd,date("YmdHis")), file_get_contents($file_path));
- //$data=$flysystem->listFiles("关键词/关键词拓展/百度/百度为您推荐");
- //var_dump($data);
- $spreadsheet->disconnectWorksheets();
- function baidu($wds)
- {
- global $deep;
- for ($i = 0; $i < $deep; $i++) {
- if (!isset($_wds)) {
- $_wds = $wds;
- }
- $wds2 = [];
- foreach ($_wds as $wd) {
- $wds2 = array_merge($wds2, getRelatedKeywords($wd));
- }
- $_wds = $wds2;
- }
- }
- function getRelatedKeywords($wd)
- {
- global $response, $result;
- $searchUrl = "http://www.baidu.com/s?ie=UTF-8&rn=20&wd=%s";
- $url = sprintf($searchUrl, $wd);
- $rules = [
- 'json' => ['.c-tools', 'data-tools'],
- ];
- $ql = (new QL\QueryList)->get($url);
- $rt = $ql->rules($rules)->query()->getData();
- $rt = json_decode(json_encode($rt), true);
- $wds = [];
- foreach ($rt as $item) {
- $json = json_decode($item['json'], true);
- $baiduUrl = substr($json['url'], 30);
- if ($baiduUrl) {
- $_url = "https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv/link?url=%s&query=%s&cb=jQuery110206243498231922682_1558178381142&data_name=recommend_common_merger_online&format=json&t=%s";
- $_url = sprintf($_url, $baiduUrl, $wd, time() . "000");
- $data = @file_get_contents($_url);
- $pat = "#1558178381142\((.*)\)#";
- preg_match($pat, $data, $arr);
- $data = json_decode($arr[1], true);
- if ($data['data'][0]['hintData']) {
- foreach ($data['data'][0]['hintData'] as $hintDatum) {
- foreach ($hintDatum['linkInfo'] as $item) {
- if ($response[md5($item['wd'])]) {
- continue;
- }
- $response[md5($item['wd'])] = $item['wd'];
- $result[] = ['key' => $wd, 'relatedWd' => $item['wd']];
- $wds[] = $item['wd'];
- }
- foreach ($hintDatum['linkInfo2'] as $item) {
- if ($response[md5($item['wd'])]) {
- continue;
- }
- $response[md5($item['wd'])] = $item['wd'];
- $result[] = ['key' => $wd, 'relatedWd' => $item['wd']];
- $wds[] = $item['wd'];
- }
- }
- }
- // print_r($data);die();
- }
- }
- return $wds;
- }
|