In my test.xml file I have multiple Field
nodes like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<Fields>
<Field>
<ID>01258</ID>
<Name>abc</Name>
<Contact>Contact One</Contact>
</Field>
<Field>
<ID>52458</ID>
<Name>def</Name>
<Contact>Contact Two</Contact>
</Field>
<Field>
<ID>74287</ID>
<Name>xyz</Name>
<Contact>Contact Three</Contact>
</Field>
</Fields>
I would like to send each Field
node one at a time through cURL to an API. This is what I have done in my PHP:
<?php
$inputFilename = 'test.xml';
$url = 'http://api-url......' // API URL
$xml = simplexml_load_file($inputFilename);
foreach($xml->Field as $field) {
$input_xml = '<Field>'.$field.'</Field>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
Now I have a problem. $input_xml
does not seem to hold the correct xml. When I run this code, I get this error:
XML Parsing Error: junk after document element
.....
Line Number 10, Column 8:
</Field><Field>
--------------^
How do I fix this? How do I make sure $field
is actually outputting an xml?