【问题标题】:Pass variable to Google Custom Search Engine将变量传递给 Google 自定义搜索引擎
【发布时间】:2010-02-19 16:47:58
【问题描述】:

是否可以将搜索变量传递到我嵌入在我的网站上的 Google 自定义搜索引擎?我可以让搜索引擎工作,但我不能通过 POST 传递一个术语(它来自网站其他页面上的搜索按钮)

我试图破解我在这里找到的代码:http://code.google.com/apis/ajax/playground/?exp=search#hello_world

这就是我目前所拥有的......($q 是我传递给它的术语)

<script type="text/javascript">
    google.load('search', '1', {language : 'en'});

    function OnLoad()
    {
        var customSearchControl = new google.search.CustomSearchControl('***my key****');
        customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
        customSearchControl.draw('cse');
        searchControl.execute("$q");
    }
    google.setOnLoadCallback(OnLoad);
</script>   

谢谢

【问题讨论】:

    标签: search-engine google-custom-search


    【解决方案1】:

    抱歉,我知道这是一个糟糕的答案,但除了引用错误的变量名之外,您实际上已经得到了正确的答案。哦,另外,顺便说一句,我还希望您对 $q 进行某种清理,以防有人在您的表单中发布类似的内容:term"); alert("aha!

        customSearchControl.draw('cse');
        searchControl.execute("$q");
    

    应该是:

        customSearchControl.draw('cse');
        customSearchControl.execute("$q");
    

    另外,谢谢你的问题 - 我正在寻找如何自己做这个!

    【讨论】:

    • 谢谢!!我一直在为此拔头发,效果很好。再次感谢!
    【解决方案2】:

    这是为了帮助任何使用 PHP 尝试实现相同目标的人。上面的例子使用...

    customSearchControl.execute("$q");
    

    读取传入的参数。在 PHP 站点上,您将使用...

    customSearchControl.execute("<?php echo $_POST['your_paramter_name_here'];?>");
    

    如果您的参数不在帖子中,您可以使用 $_GET 或 $_REQUEST。

    当然,您应该首先清理输入。像这样的东西很弱,但这是一个开始......

    customSearchControl.execute("<?php echo htmlentities( trim( $_POST['your_paramter_name_here'] ), ENT_QUOTES );?>");
    

    【讨论】:

      【解决方案3】:

      如果有人正在寻找更直接/简单的解决方案。您所要做的就是将搜索关键字传递给名为 q 的 GET 参数(从您的自定义表单到您的 GCS 所在的页面),GCS 将自动使用该搜索短语。

      来源:https://developers.google.com/custom-search/json-api/v1/using_rest

      【讨论】:

      • 多么简单的解决方案啊。 site.com/search?q=query 将自动使用该查询进行搜索。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2019-11-07
      • 2014-06-17
      相关资源
      最近更新 更多