欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP获取百度关键词排名

程序员文章站 2024-01-25 18:17:28
...
PHP获取百度关键词排名

PHP代码

@$k=$_GET['k'] ? $_GET['k'] : '不念博客';
@$u=$_GET['u'] ? $_GET['u'] : 'www.bunian.cn';
$rn='50';
$url = "https://www.baidu.com/s?ie=utf-8&wd=".$k."&rn=".$rn;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
preg_match_all('/<div class=\"f13\">(.*?)<\/div>/si', $res, $p);
for ($ii = 0; $ii < COUNT($p[1]); $ii++){
$pos=strpos($p[1][$ii],$u);
if(is_integer($pos)){
$rn= $ii+1;
break;
}
}
echo $rn;
相关标签: PHP