简体   繁体   English

使用REST在PHP中使用Httpful发送XML

[英]Using REST to Send XML in PHP using Httpful

I managed to get a SOAP client working for a client but I am now trying to use REST for a slightly different project. 我设法让一个SOAP客户端为客户端工作,但我现在正尝试将REST用于稍微不同的项目。 I am using Httpful. 我正在使用Httpful。 From what I have read it is what I need in order to do what I've been asked. 从我所读到的,我需要的是为了做我被问过的事情。

This is my code so far. 到目前为止这是我的代码。

/* 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!';
    }
}

The guy I am working with doesn't really know a lot about REST either but apparently there is a way to retrieve a response from the request being sent. 我正在与之合作的人对REST也不是很了解,但显然有一种方法可以从发送的请求中检索响应。 Does anyone know, or at least can point me in the right direction, to solve this? 有谁知道,或者至少可以指出我正确的方向,来解决这个问题?

Thanks. 谢谢。

EDIT: Sorry, just to elaborate. 编辑:对不起,只是详细说明。 The XML is placed into the textarea field then validated against an XSD file. XML被放入textarea字段,然后根据XSD文件进行验证。 If this is successful it will then post that XML to the REST endpoint. 如果成功,则会将该XML发布到REST端点。

the return value from the send call (named $request in your code) is actually a Httpful::Response Object and contains the response from your http-call. 发送调用的返回值(代码中名为$ request)实际上是一个Httpful :: Response对象,包含来自http-call的响应。

Try echo $request->body; 尝试echo $ request-> body; instead of echo 'SENT!'; 而不是echo'SENT!';

And remember to always write the block for if-expressions, you can create hard to find bugs by skipping it :) 并且记得总是为if-expressions编写块,你可以通过跳过它来创建很难找到bug :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM