thinkphp获取百度关键字搜索下的网站排名
1.使用composer安装QueryList
composer require jaeger/querylist
2.引入QueryList到开发类中使用
编写类文件
<?php
/**
* Created by PhpStorm.
* User: 张旭
* Date: 2019/11/29
* Time: 17:20
*/
namespace app\index\controller;
use QL\QueryList;
use think\facade\View;
class Index
{
public function WebTop()
{
$url='zhangxuu.com'; //要检测的域名
$rn = 50; //搜索数量
$Keyword='张旭php'; // 搜索关键字
$html = QueryList::get('https://www.baidu.com/s?wd='.$Keyword.'&rn='.$rn);
$urls = $html->find('#content_left>.c-container>.f13>.c-showurl')->texts()->toArray();
foreach ($urls as $k => $v){
$urls[$k] = str_replace('http://','',$urls[$k]);
$urls[$k] = str_replace('https://','',$urls[$k]);
$urls[$k] = str_replace('www.','',$urls[$k]);
$urls[$k] = str_replace('/','',$urls[$k]);
$urls[$k] = str_replace('...','',$urls[$k]);
$urls[$k] = str_replace('"','',$urls[$k]);
$urls[$k] = trim($urls[$k],chr(0xc2) . chr(0xa0));
}
if (!in_array($url,$urls)){
$msg = '在关键字['.$Keyword.']搜索下,您的网站域名'.$url.'百度排名'.$rn.'名开外';
}else{
$urls = array_flip($urls);
$msg = '在关键字['.$Keyword.']搜索下,您的网站域名'.$url.'百度排名为第'.($urls[$url]+1);
}
return $msg;
}
}