提交Google自定义搜索

时间:2018-08-21 16:52:30

标签: php forms submit google-custom-search

我正在使用不再可用的旧Google自定义搜索代码,我想检查如何解决该代码以使其与新的Google自定义搜索一起使用。

我没有运气就尝试了很多事情,这是我今天拥有的代码:

// Google Search (deprecated - to remove)
if ($task == 'search' && $params->get('googleSearch'))
{
    $language = JFactory::getLanguage();
    $lang = $language->getTag();
    // Fallback to the new container ID without breaking things
    $googleSearchContainerID = trim($params->get('googleSearchContainer', 'k2GoogleSearchContainer'));
    if($googleSearchContainerID == 'k2Container'){
        $googleSearchContainerID = 'k2GoogleSearchContainer';
    }
    $document->addScript('https://www.google.com/jsapi');
    $document->addScriptDeclaration('
        google.load("search", "1", {"language" : "'.$lang.'"});
        function OnLoad(){
            var searchControl = new google.search.SearchControl();
            var siteSearch = new google.search.WebSearch();
            siteSearch.setUserDefinedLabel("'.$application->getCfg('sitename').'");
            siteSearch.setUserDefinedClassSuffix("k2");
            options = new google.search.SearcherOptions();
            options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
            siteSearch.setSiteRestriction("'.JURI::root().'");
            searchControl.addSearcher(siteSearch, options);
            searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
            searchControl.setLinkTarget(google.search.Search.LINK_TARGET_SELF);
            searchControl.draw(document.getElementById("'.$googleSearchContainerID.'"));
            searchControl.execute("'.JRequest::getString('searchword').'");
        }
        google.setOnLoadCallback(OnLoad);

2 个答案:

答案 0 :(得分:0)

当前的“自定义搜索”元素API文档可在以下位置找到: https://developers.google.com/custom-search/docs/element

答案 1 :(得分:0)

将旧系统更新为新。易于使用。

关注此链接; https://developers.google.com/custom-search/docs/tutorial/creatingcse

然后点击此 控制面板 带有此链接:https://cse.google.com/create/new 创建之后在您的网站中使用以下enter code here代码:

<script>
  (function() {
    var cx = '012345678901234567890:inyoqlhp8ya'; /* 21 Numbers code */
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search></gcse:search>