【问题标题】:Using REST to Send XML in PHP using Httpful使用 REST 在 PHP 中使用 Httpful 发送 XML
【发布时间】:2013-04-23 22:25:53
【问题描述】:

我设法让 SOAP 客户端为客户端工作,但我现在正尝试将 REST 用于稍微不同的项目。我正在使用 Httpful。根据我所阅读的内容,我需要这样做才能完成我被要求做的事情。

这是我目前的代码。

/* MAIN RESTFUL */

$xml_file = './Project.xml';
$xsd_file = './Project.xsd';
$end_uri = 'https://****.com/Service.svc/CreateProject';

/* TEMP TEXTAREA FIELD */

if(!isset($_POST['submit']))
    $_xml = file_get_contents($xml_file);
else
    $_xml = $_POST['xml_input'];

echo '<form method="post" action=""><textarea name="xml_input" rows="10" cols="100">' . $_xml . '</textarea><br /><input type="submit" name="submit" value="Send"></form><br /><a href="./">Reset Form</a><br /><br />';

/* END TEMP TEXTAREA FIELD */

if(isset($_POST['submit']))
    $xml_file = $_POST['xml_input'];

if(isset($_POST['submit']))
{
    $xsd_validate = new DOMDocument();
    $xsd_validate->loadXML($xml_file);

    if($xsd_validate->schemaValidate($xsd_file))
        $sendXML = true;

    if($sendXML == true)
    {
        require('./httpful-master/bootstrap.php');

        $request = \Httpful\Request::post($end_uri)
            ->body($xml_file)
            ->sendsXml()
            ->send();

        if($request)
            echo 'SENT!';
    }
}

与我一起工作的人也不太了解 REST,但显然有一种方法可以从正在发送的请求中检索响应。有谁知道,或者至少可以指出我正确的方向来解决这个问题?

谢谢。

编辑:对不起,只是为了详细说明。 XML 被放入 textarea 字段,然后根据 XSD 文件进行验证。如果成功,它将将该 XML 发布到 REST 端点。

【问题讨论】:

    标签: php rest httpful


    【解决方案1】:

    发送调用的返回值(在您的代码中命名为 $request)实际上是一个 Httpful::Response 对象并包含来自您的 http 调用的响应。

    试试 echo $request->body;而不是 echo 'SENT!';

    记住要始终为 if 表达式编写代码块,跳过它会造成难以发现的错误 :)

    【讨论】:

    • 很抱歉回复晚了,但这正是它的本意。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2013-07-21
    • 1970-01-01
    相关资源
    最近更新 更多