【问题标题】:Submitting a Google Search Appliance (GSA) Content Feed with PHP cURL - 400 Error使用 PHP cURL 提交 Google Search Appliance (GSA) 内容提要 - 400 错误
【发布时间】:2014-11-19 05:13:17
【问题描述】:

因此,我正在尝试为我的 Web 应用程序开发一个模块,该模块使用 PHP cURL 将内容提要推送到我们的 Google Search Appliance (GSA),以通过端口 19900 将数据作为 POST 信息传输到设备。基于我所做的一切已经阅读了有关创建和提交给 GSA 的提要的文档,这应该可以正常工作,但是服务器返回以下(非常模糊且基本上无用)错误:

  1. 这是一个错误。

您的客户发出了格式错误或非法的请求。这就是我们所知道的。

我一直在与帮助我们姊妹站点上的 GSA 检测的架构师一起解决此问题,但我们无法确定是什么导致了我们的问题。根据我们的 IT 部门的说法,所有端口都已打开以进行此通信(如果它们关闭,我们将不会收到错误消息),并且我们已验证发送服务器的 IP 地址被列为“允许”在 GSA 中。不用说,我们被难住了。

这是传输 XML 提要的代码:

<?php
$target_url = 'http://gsadomain.com:19900/xmlfeed';

$header = array('Content-Type: multipart/form-data');

$fields = array(
    'feedtype'=>'incremental',
    'datasource'=>'datasourcename',
    'data'=>'@'.realpath('gsa_feed.xml')
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERPWD, "gsaadmin:gsaadminpassword");
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_TIMEOUT,120);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

$return = curl_exec($ch);

if (curl_errno($ch)) {
    $msg = curl_error($ch);
}

curl_close ($ch);

echo $return;
?>

这是我们尝试提交的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE gsafeed PUBLIC "-//Google//DTD GSA Feeds//EN" "http://this.is.the.ip/gsafeed.dtd">
<gsafeed>
    <header>
        <datasource>datasource</datasource>
        <feedtype>incremental</feedtype>
    </header>
    <group>
        <record url="http://website.com/mod/view.php?id=15903" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">customers</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">partners</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
            </acl>
            <metadata>
                <meta name="type" content="module" />
                <meta name="id" content="1" />
                <meta name="name" content="Module for Everyone" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Module for Everyone.
        </content>
        </record>
        <record url="http://website.com/mod/view.php?id=15904" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">partners</principal>
            </acl>
            <metadata>
                <meta name="type" content="module" />
                <meta name="id" content="2" />
                <meta name="name" content="Module for Partners" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Module for Partners.
        </content>
        </record>
        <record url="http://website.com/mod/view.php?id=15905" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
            </acl>
            <metadata>
                <meta name="type" content="module" />
                <meta name="id" content="3" />
                <meta name="name" content="Module for Employees" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Module for Employees.
        </content>
        </record>
        <record url="http://website.com/course/view.php?id=655#section-1" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">customers</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">partners</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
            </acl>
            <metadata>
                <meta name="type" content="topic" />
                <meta name="id" content="1" />
                <meta name="name" content="Course Topic for Everyone" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Course Topic for All Audiences.
        </content>
        </record>
        <record url="http://website.com/course/view.php?id=655#section-2" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">partners</principal>
            </acl>
            <metadata>
                <meta name="type" content="topic" />
                <meta name="id" content="2" />
                <meta name="name" content="Course Topic for Partners" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Course Topic for Partners.
        </content>
        </record>
        <record url="http://website.com/course/view.php?id=655#section-3" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
            </acl>
            <metadata>
                <meta name="type" content="topic" />
                <meta name="id" content="3" />
                <meta name="name" content="Course Topic for Employees" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Course Topic for Employees.
        </content>
        </record>
        <record url="http://website.com/course/view.php?id=655" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">customers</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">partners</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
            </acl>
            <metadata>
                <meta name="type" content="course" />
                <meta name="id" content="655" />
                <meta name="name" content="Course for Everyone" />
                <meta name="course_id" content="655" />
            </metadata>
            <content>
                This is the description of the Course for Everyone.
        </content>
        </record>
        <record url="http://website.com/course/view.php?id=656" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">partners</principal>
            </acl>
            <metadata>
                <meta name="type" content="course" />
                <meta name="id" content="656" />
                <meta name="name" content="Course for Partners" />
                <meta name="course_id" content="656" />
            </metadata>
            <content>
                This is the description of the Course for Partners.
        </content>
        </record>
        <record url="http://website.com/course/view.php?id=657" mimetype="text/html" last-modified="Thu, 14 Aug 2014 18:53:00 GMT">
            <acl inheritance-type="and-both-permit">
                <principal scope="group" access="permit" namespace="Default" case-sensitivity-type="everything-case-insensitive">employees</principal>
            </acl>
            <metadata>
                <meta name="type" content="course" />
                <meta name="id" content="657" />
                <meta name="name" content="Course for Employees" />
                <meta name="course_id" content="657" />
            </metadata>
            <content>
                This is the description of the Course for Employees.
        </content>
        </record>
    </group>
</gsafeed>

根据我们所看到的一切,这应该可行,但我们遇到了砖墙。有人有什么想法吗?

作为补充说明,由于我们尝试建立索引的页面的设置方式,让设备抓取页面将无法正常工作(交互式元素太多,我读过的所有内容都表明 GSA无法正确索引这些)。

编辑 1:正如 Mark 在回复中所建议的,这里是 GSA Feed 开发人员指南的链接:http://www.google.com/support/enterprise/static/gsa/docs/admin/72/gsa_doc_set/feedsguide/feedsguide.html

编辑 2:成功!请看下面我的回答。关键是让 cURL 处理 $fields 数组的编码,并传递文件内容,而不仅仅是文件路径。

【问题讨论】:

    标签: php curl google-search-appliance


    【解决方案1】:

    所以,长话短说,我能够正确提交提要,而这一切都与 cURL 处理数据的方式有关。我和与我一起尝试提交提要的工程师都没有太多使用 PHP 的 cURL 插件的经验,也不知道如何让 GSA 接受输入字段。部分感谢this question by Kenanswer by ThiefMasternext answer from Czechnology,以及上面 Mike 的帮助,我想出了以下代码:

    <?php
    $target_url = 'http://gsadomain.com:19900/xmlfeed';
    
    $header = array('Content-Type: multipart/form-data');
    
    $fields = array(
        'feedtype'=>'incremental',
        'datasource'=>'datasourcename',
        'data'=>file_get_contents(realpath('gsa_feed.xml'))
    );
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_USERPWD, "gsaadmin:gsaadminpassword");
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch, CURLOPT_TIMEOUT,120);
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    
    $return = curl_exec($ch);
    
    if (curl_errno($ch)) {
        $msg = curl_error($ch);
    }
    
    curl_close ($ch);
    
    echo $return;
    ?>
    

    困难在于 http_build_query() 方法,它试图为它完成 cURL 的工作,并且没有正确设置 POST 数据的边界。

    我们后来在处理 XML 中的某些字段时也遇到了一些困难,但这主要是因为我们忘记了 str_replace 和符号、单引号和双引号。处理完这些之后,XML 就被正确解析了,一切都运行起来了。

    【讨论】:

      【解决方案2】:

      我不熟悉 GSA API,但您似乎并未真正发送任何 XML 数据。您将为data 参数发送的字符串值将类似于@/path/to/gsa_feed.xml。我想您实际上需要发布 XML 对吗?

      也许更像

      $fields = array(
          'feedtype'=>'incremental',
          'datasource'=>'datasourcename',
          'data'=> file_get_contents(realpath('gsa_feed.xml'))
      );
      

      【讨论】:

      • 我只是试了一下,没有运气。有问题的 PHP 脚本构建了 xml 文件,所以最初,我只是将写入文件的字符串包含为 '$fields = array('fieldtype'=>'incremental','datasource'=>'datasourcename','数据'=>$string);'当这不起作用时,我切换到 @path/to/gsa_feed.xml 方法(尽管鉴于我在文档中看到的内容,您的建议可能是我想要的。
      • @JoshC 您可以在问题中链接 API 文档。在您展示的 PHP 中,没有任何地方可以构建 XML 文件。您显示的只是一个简单的 cURL POST,其中包含一些数据字段,其中一个是有问题的 this 字符串。除此以外,这里没有任何建议的功能。
      • @Mark Brant 我刚刚编辑了原始帖子以包含指向 Google 开发人员指南的链接。不幸的是,它并没有在设置您自己的提要脚本方面提供太多指导。这是链接:google.com/support/enterprise/static/gsa/docs/admin/72/…
      猜你喜欢
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多