效果:
这里有我做的示例:http://www.wqit.net/seoorder.php
HTML
<script src="static/js/seoorder.js"></script>
<table class="data">
<tr>
<td colspan="5" class="tip">
提示:因搜索引擎的不可控性,排名可能会突然上升、下降或各地区的查询结果不相同,以下信息仅供临时参考。<br />
本次查询地点:河南郑州 <a href="http://www.baidu.com/" target="_blank">[打开百度]</a>
<a href="http://www.soso.com/" target="_blank">[打开搜搜]</a>
<a href="javascript:void(0);" id="search" class="more">【点击查询排名】</a>
</td>
</tr>
<tr>
<th scope="col">序号</th>
<th scope="col">客户名称</th>
<th scope="col">关健字</th>
<th scope="col">百度排名</th>
<th scope="col">搜搜排名</th>
</tr>
<?php
// ....
while( $r = /*....*/ ){
$i++;
?>
<tr>
<td class="center"><?php echo $i;?></td>
<td><?php echo $r['title'];?></td>
<td class="center"><?php echo $r['keywords'];?></td>
<td class="center"><div class="baiduLoading" word="$r['keywords'];?>" url="<?php echo $r['linkurl'];?>">准备查询...</div></td>
<td class="center"><div class="sosoLoading" word="<?php echo $r['keywords'];?>" url="<?php echo $r['linkurl'];?>">准备查询...</div></td>
</tr>
<?php
}
?>
</table>
JS
seoorder.js
$(function(){
$('#search').click(function(){
$("table div").html('正在查询...');
// baidu
$("table div.baiduLoading").each(function(i){
var word = $(this).attr('word');
var url = $(this).attr('url');
$.get("/plugin/seo/getBaiduOrder.php?key="+word+"&url="+url, function(data){
$("table div.baiduLoading").each(function(j){
if(i == j) $(this).html(data);
});
},'html');
word = url = '';
});
// soso
$("table div.sosoLoading").each(function(i){
var word = $(this).attr('word');
var url = $(this).attr('url');
$.get("/plugin/seo/getSosoOrder.php?key="+word+"&url="+url, function(data){
$("table div.sosoLoading").each(function(j){
if(i == j) $(this).html(data);
});
},'html');
word = url = '';
});
});
});
PHP
getBaiduOrder.php
<?php
// ?key=string&url=www.abc.com
$key = isset($_GET['key']) ? trim($_GET['key']) : '';
$url = isset($_GET['url']) ? trim($_GET['url']) : '';
if(empty($key) || empty($url)){
exit('Error');
}else{
if( count(explode('.',$url)) <= 2){
$url = ltrim($url,'http://');
$url = 'www.' . $url;
}
s($key,$url);
}
function s($keyword,$url,$page = 1){
$rsState = false;
$enKeyword = urlencode($keyword);
$firstRow = ($page - 1) * 10;
if($page > 10){
echo '<span style="color:#CCC;">10页内暂无排名</span>';
}else{
$contents = @file_get_contents("http://www.baidu.com/s?wd=$enKeyword&pn=$firstRow");
if(!$contents) exit('查询超时');
preg_match_all('/<table[^>]*?class="result"[^>]*>[\s\S]*?<\/table>/i',$contents,$rs);
foreach($rs[0] as $k=>$v){
if(strstr($v,$url)){
$rsState = true;
echo '
<a target="_blank" title="进入百度查看" href="http://www.baidu.com/s?wd='.$enKeyword.'&pn='.$firstRow.'">第' . $page . '页,第' . ++$k . "个</a>";
break;
}
}
unset($contents);
if($rsState === false){
s($keyword, $url,++$page);
}
}
}
?>
getSosoOrder.php
<?php
// ?key=string&url=www.abc.com
$key = isset($_GET['key']) ? trim($_GET['key']) : '';
$url = isset($_GET['url']) ? trim($_GET['url']) : '';
if(empty($key) || empty($url)){
exit('Error');
}else{
if( count(explode('.',$url)) <= 2){
$url = ltrim($url,'http://');
$url = 'www.' . $url;
}
s($key,$url);
}
function s($keyword,$url,$page = 1){
$rsState = false;
$enKeyword = urlencode($keyword);
$firstRow = $page;//($page - 1) * 10;
if($page > 10){
echo '<span style="color:#CCC;">10页暂内无排名</span>';
}else{
//http://www.soso.com/q?w=$enKeyword&lr=&sc=web&ch=w.p&num=10&gid=&cin=&site=&sf=0&sd=0&nf=0&pg=$firstRow
$contents = @file_get_contents("http://www.soso.com/q?w=$enKeyword&lr=&sc=web&ch=w.p&num=10&gid=&cin=&site=&sf=0&sd=0&nf=0&pg=$firstRow");
if(!$contents) exit('查询超时');
preg_match_all('/<li[^>]*?loc="*?"[^>]*>[\s\S]*?<\/li>/i',$contents,$rs);
foreach($rs[0] as $k=>$v){
if(strstr($v,$url)){
$rsState = true;
echo '
<a target="_blank" title="进入搜搜查看" href="http://www.soso.com/q?w='
.$enKeyword.'&lr=&sc=web&ch=w.p&num=10&gid=&cin=&site=&sf=0&sd=0&nf=0&pg='.$firstRow.
'">第' . $page . '页,第' . ++$k . "个</a>";
break;
}
}
unset($contents);
if($rsState === false){
s($keyword, $url,++$page);
}
}
}
?>
<完>
