0

I want to generate a dynamic xml to send to a webservice. I have a php foreach loop inside the xml. But I can't get the syntax right.

I need the string to look like this -

<?xml version=1.0" encoding="ISO-8859-1">
<inventoryUpdateRequest version="1.0">
<action name="bookupdate">
</action>
<SiteNameList>
<SiteName>
<vendorBookID>123456</vendorBookID>
</SiteName>
<SiteName>
<vendorBookID>788901</vendorBookID>
</SiteName>
</SiteNameList>
</inventoryUpdateRequest>

The <SiteName>...</SiteName> is looped for each value found.

My code so far is this -

<?php 
$xml =
'<?xml version="1.0" encoding="ISO-8859-1">
<inventoryUpdateRequest version="1.0">
<action name="bookupdate">
</action>
<SiteNameList>
'.
$ids_orderlineitem_portal_rownum = 1;
foreach($ids_row['order_line_item::bookID'] as  $ids_orderlineitem_portal_row=>$ids_orderlineitem_portal)
{
.'
<SiteName>
<vendorBookID>
'.$ids_row['order_line_item::bookID'][$ids_orderlineitem_portal_row].'
</vendorBookID>
</SiteName>
'.if($ids_orderlineitem_portal_rownum == 0) break; else  $ids_orderlineitem_portal_rownum++;
}//portal_end
.'
</SiteNameList>
</inventoryUpdateRequest>'
;
$url ='https://inventoryupdate.sitename.com';
$port = 80;

$response = xml_post($xml, $url, $port);    
?>

Help? How can I get the loop to output the format I need?

Lily
  • 11
  • 1
  • 2
  • Use one of the XML extensions. See http://stackoverflow.com/questions/2060346/php-what-is-the-best-approach-to-using-xml-need-to-create-and-parse-xml-response – Gordon May 22 '11 at 21:46

1 Answers1

1

It's hard to know if this will work, because I don't know what format the input data is in , but hopefully this should work, or is at least closer to what you need:

<?php 
    $xml =
    '<?xml version="1.0" encoding="ISO-8859-1">
    <inventoryUpdateRequest version="1.0">
    <action name="bookupdate">
    </action>
    <SiteNameList>';
    foreach($ids_row['order_line_item::bookID'] as  $book_id)
    {
        $xml .= '
        <SiteName>
        <vendorBookID>
        '.$book_id.'
        </vendorBookID>
        </SiteName>';
    }//portal_end
    $xml .= '
    </SiteNameList>
    </inventoryUpdateRequest>'
    ;

    $url ='https://inventoryupdate.sitename.com';
    $port = 80;

    $response = xml_post($xml, $url, $port);    
?>
Jodes
  • 14,118
  • 26
  • 97
  • 156
  • Jodes, tried your method, source code shows correct xml format and info, but response from webservice says XML error. Grr, feels like the solution is so close! Thanks for the help. – Lily May 23 '11 at 00:54
  • OK, so I figured it out, I was missing a "?" at the end of the first line. And so it works beautifully now. Thank you Jodes, my hero! – Lily May 23 '11 at 01:46