6

是否可以将搜索变量传递到我嵌入在我的网站上的 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>   

谢谢

4

3 回答 3

6

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

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

应该:

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

另外,谢谢您的问题-我正在寻找自己的方法!

于 2010-03-08T13:55:01.217 回答
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 );?>");
于 2011-04-12T20:02:27.063 回答
2

In case someone is looking for a bit more straight forward / simple solution. All you have to do is to pass search keywords into GET parameter named q (from your custom form into page where your GCS is), GCS will automatically use that search phrase.

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

于 2015-04-20T14:33:18.913 回答