详情页

帝国cms相关搜索功能扩展(利用百度下实现)

时间:2023年09月17日

编辑:佚名

类似下面这样,根据标题获取一批相关的词,然后开启搜索功能,点击这个就能跳转到帝国cms搜索页面,前提是你帝国cms的搜索功能经过改良,可以模糊搜索!

PHP获取百度下拉
function getBaiduSuggestion($title) {
    $title = urlencode($title);
    $api = "https://www.baidu.com/sugrec?pre=1&p=1&ie=utf-8&prod=pc&csor=2&wd=".$title."";
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $api,
        CURLOPT_TIMEOUT => 5,
        CURLOPT_RETURNTRANSFER => true,
    ]);
    $r = curl_exec($ch);
    curl_close($ch);
    $r = json_decode($r, true);
    if (!empty($r['g'])) {
        return array_column($r['g'], 'q');
    } else {
        return false;
    }
}
帝国内容模板调用
<h3>相关搜索</h3>
<style>
.searchA{
  padding: 0 10px 6px 0;
  display: block;
}
</style>
<?php 
$otherSearch = getBaiduSuggestion($navinfor['title']);
if($otherSearch){
  foreach($otherSearch as $v){
    echo "<a class='searchA' href='[!--news.url--]moreSearch/".urlencode($v).".html'>$v</a>";
  }
}
?>
伪静态规则修改
rewrite ^/moreSearch/(.*?)\.html$ /e/search/index.php?searchget=1&keyboard=$1&show=title&tempid=1&dopost=search last;
搜索页面返回的代码修改
Header("Location:$public_r[newsurl]e/search/result/?searchid=$searchid".$dogetvar);
相关文章
猜你需要