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?

1

1 Answer 1

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);    
?>
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.