【问题标题】:Submit Google Custom Search提交 Google 自定义搜索
【发布时间】:2018-08-21 16:52:29
【问题描述】:

我正在使用不再工作的旧 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);

【问题讨论】:

    标签: php forms submit google-custom-search


    【解决方案1】:

    当前的自定义搜索元素 API 文档可在此处找到: https://developers.google.com/custom-search/docs/element

    【讨论】:

    • 谢谢安迪,我知道,但我尝试用新的谷歌代码更新我的代码
    【解决方案2】:

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

    点击此链接; 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>
    

    【讨论】:

      猜你喜欢
      • 2011-08-16
      • 2019-07-17
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多