在heihe view相关搜索中,调整了google ajax搜索,增加了bing ajax搜索和cnki ajax搜索功能

This commit is contained in:
Li Jianxuan 2011-11-22 03:12:15 +00:00
parent 3936cc94b4
commit f2ea852aae
2 changed files with 154 additions and 16 deletions

View File

@ -767,6 +767,7 @@ class ServiceController extends Zend_Controller_Action
'q' => $q, // 查询内容 'q' => $q, // 查询内容
'key' => $this->config->google->maps->api, 'key' => $this->config->google->maps->api,
'userip' => $_SERVER['REMOTE_ADDR'], 'userip' => $_SERVER['REMOTE_ADDR'],
'rsz' => 5
)); ));
print_r($rez); print_r($rez);
exit(); exit();
@ -784,6 +785,81 @@ class ServiceController extends Zend_Controller_Action
$body = curl_exec($ch); $body = curl_exec($ch);
curl_close($ch); curl_close($ch);
return $body; return $body;
} }// google 搜索
function bingsearchAction(){
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$q = $this->_request->getParam('q');
$rez = $this->bing_search($q);
print_r($rez);
exit();
}
function bing_search($keyword,$referer = 'http://westdc.westgis.ac.cn/'){
$keyword=urlencode($keyword);
$url = "http://api.bing.net/json.aspx?AppId=".$this->config->bing->api
."&Query=".$keyword
."&Sources=Web"
."&Version=2.0"
."&Market=en-us"
."&Adult=Moderate"
."&Options=EnableHighlighting"
."&Web.Count=5"
."&Web.Offset=0"
."&Web.Options=DisableHostCollapsing+DisableQueryAlterations";
//&JsonType=callback&JsonCallback=SearchCompleted
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
return $body;
}//bing 搜索
function cnkisearchAction(){
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$q = $this->_request->getParam('q');
$rez = $this->cnki_search($q);
print_r($rez);
exit();
}
function cnki_search($keyword , $referer = 'http://westdc.westgis.ac.cn/'){
$keyword = urlencode($keyword);
$url = "http://search.cnki.net/search.aspx?q=$keyword";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
$rev = array();
$max = 5;
if(preg_match_all("#<a.+? class=\"acticle\" href=\"(.+?)\".*?>(.+?)</a>#i",$body,$matchs,PREG_PATTERN_ORDER))
{
foreach ($matchs[2] as $k=>$v){
if($k < $max) $rev[$k]['title']=$v;
}
foreach ($matchs[1] as $k=>$v){
if($k < $max) $rev[$k]['url']=$v;
}
}
if(preg_match_all("#<span class=\"search_result_snap\" >(.+?)</span>#i",$body,$matchs,PREG_PATTERN_ORDER))
{
foreach ($matchs[1] as $k=>$v){
if($k < $max) $rev[$k]['content']=$v;
}
}
$response=array("result"=>$rev,"count"=>$max,"morelink"=>'http://search.cnki.net/search.aspx?q='.$keyword);
$rev = Zend_Json::encode($response);
return $rev;
}//cnki 搜索
} }

View File

@ -229,12 +229,20 @@ endforeach;
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php
$keywords = array();
foreach($this->keys as $cg){
if($cg->keytype=="theme")
$keywords[]=$cg->keyword;
}
$ev = join(" ",$keywords);
?>
<ul class="idTabs"> <ul class="idTabs">
<li><a href="#related">相关数据</a></li> <li><a href="#related">相关数据</a></li>
<li><a href="#literature">相关文献</a></li> <li><a href="#literature">相关文献</a></li>
<li><a href="#comments">数据评论</a></li> <li><a href="#comments">数据评论</a></li>
<li><a href="#service">服务记录</a></li> <li><a href="#service">服务记录</a></li>
<li><a href="#gsearch">相关搜索</a></li> <li><a href="#gsearch" class="googlesearch">相关搜索</a></li>
<li><a href="#recommend">给我推荐</a></li> <li><a href="#recommend">给我推荐</a></li>
</ul> </ul>
<?php if ($this->related) : ?> <?php if ($this->related) : ?>
@ -278,15 +286,9 @@ echo '【'.$r->author.'. '.$r->publisher.' '.$r->ts_issued.'】</li>';
</div> </div>
<div id="gsearch"> <div id="gsearch">
<ul id="searchctrl"> <ul id="searchctrl">
<li class="title">Google关键词搜索:</li> <li><a href="javascript:;" class="googlesearch">Google关键词搜索</a></li>
<?php <li><a href="javascript:;" class="cnkiSearch">CNKI知识搜索</a></li>
foreach($this->keys as $cg){ <li><a href="javascript:;" class="bingsearch">CN bing 搜索</a></li>
echo '<li><a href="javascript:;" onclick="googlesearch(\''.$cg->keyword.'\');">'.$cg->keyword.'</a></li>'."\r\n";
}
?>
<li class="title">文献搜索:</li>
<li><a href="http://search.cnki.net/search.aspx?q=<?php echo urlencode($this->escape($md->title));?>" target="_blank">CNKI知识搜索</a></li>
<li><a href="http://cn.bing.com/search?q=<?php echo urlencode($this->escape($md->title));?>&go=&qs=n&sk=&form=QBLH" target="_blank">CN bing 搜索</a></li>
</ul> </ul>
<ul id="searchlist"> <ul id="searchlist">
</ul> </ul>
@ -356,7 +358,8 @@ function ajaxpage(page){
success:function(html){$('#allcomments').html(html);}, success:function(html){$('#allcomments').html(html);},
beforeSend:function(){$('#allcomments').html('<img src="/images/loading.gif" />评论加载中');} beforeSend:function(){$('#allcomments').html('<img src="/images/loading.gif" />评论加载中');}
}); });
};ajaxpage(0); };
ajaxpage(0);
function getFileList(){ function getFileList(){
var tw = ($(window).width() - $('#window-outter').width())/2; var tw = ($(window).width() - $('#window-outter').width())/2;
$('#window-outter').css({'right':tw}); $('#window-outter').css({'right':tw});
@ -411,8 +414,17 @@ function getSubFileList(uuid,id,depth){
}); });
} }
$('.googlesearch').click(function(){
googleSearch('<?php echo $ev;?>');
});
$('.bingsearch').click(function(){
bingSearch('<?php echo $ev;?>');
});
$('.cnkiSearch').click(function(){
cnkiSearch('<?php echo $ev;?>');
});
function googlesearch(keyword){ function googleSearch(keyword){
$.ajax({ $.ajax({
type:"POST", type:"POST",
url:"/service/googlesearch", url:"/service/googlesearch",
@ -421,12 +433,62 @@ function googlesearch(keyword){
var items = []; var items = [];
var obj = jQuery.parseJSON(html); var obj = jQuery.parseJSON(html);
$.each(obj.responseData.results, function(key, val) { $.each(obj.responseData.results, function(key, val) {
var html = '<p><a href="' +val['unescapedUrl']+'">'+val['title']+'<a/></p><p><span>'+ var html = '<p><a href="' +val['unescapedUrl']+'">'+val['title']+'<a/></p><p><span>'
val['url'].replace(/%(.*)/i,"") +'</span></p><p>'+ +val['url'].replace(/%(.*)/i,"") +'</span></p><p>'
val['content'] +val['content']
+'</p>'; +'</p>';
items.push('<li>' + html + '</li>'); items.push('<li>' + html + '</li>');
}); });
items.push('<li class="more"><a href="'+obj.responseData.cursor.moreResultsUrl+'" target="_blank">查看更多搜索结果(约'+obj.responseData.cursor.estimatedResultCount+'条)</a></div>');
$('#searchlist').html(items.join(''));
},
beforeSend:function(){
$('#searchlist').html('<img src="/images/11887177066.gif" />结果加载中');
}
});
}
function bingSearch(keyword){
$.ajax({
type:"POST",
url:"/service/bingsearch",
data:'q='+keyword,
success:function(html){
var items = [];
var obj = jQuery.parseJSON(html);
$.each(obj.SearchResponse.Web.Results, function(key, val) {
var html = '<p><a href="' +val['Url']+'">'+val['Title']+'<a/></p><p><span>'
+val['DisplayUrl'].replace(/%(.*)/i,"") +'</span></p><p>'
+val['Description']
+'</p>';
items.push('<li>' + html + '</li>');
});
items.push('<li class="more"><a href="http://cn.bing.com/search?q=<?php echo urlencode($ev); ?>&go=&qs=n&sk=&form=QBLH" target="_blank">查看更多搜索结果(约'+obj.SearchResponse.Web.Total+'条)</a></div>');
$('#searchlist').html(items.join(''));
},
beforeSend:function(){
$('#searchlist').html('<img src="/images/11887177066.gif" />结果加载中');
}
});
}
function cnkiSearch(keyword){
$.ajax({
type:"POST",
url:"/service/cnkisearch",
data:'q='+keyword,
success:function(html){
var items = [];
var obj = jQuery.parseJSON(html);
$.each(obj.result, function(key, val) {
var html = '<p><a href="' +val['url']+'" target="_blank">'+val['title']+'<a/></p><p><span>'
+val['url'].replace(/%(.*)/i,"") +'</span></p><p>'
+val['content']
+'</p>';
items.push('<li>' + html + '</li>');
});
items.push('<li class="more"><a href="'+obj.morelink+'" target="_blank">查看更多搜索结果</a></div>');
$('#searchlist').html(items.join('')); $('#searchlist').html(items.join(''));
}, },
beforeSend:function(){ beforeSend:function(){