简体   繁体   中英

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. I am using 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. 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. If this is successful it will then post that XML to the REST endpoint.

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.

Try echo $request->body; instead of echo 'SENT!';

And remember to always write the block for if-expressions, you can create hard to find bugs by skipping it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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