Reputation: 363
See below, the XML body I have :
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://start.exactonline.be/api/v1/393999/sync/logistics/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">SalesItemPrices</title>
<link rel="self" title="SalesItemPrices" href="SalesItemPrices" />
<entry>
<id>https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117417L)</id>
<title type="text"></title>
<updated>2021-08-11T11:47:22Z</updated>
<author>
<name />
</author>
<link rel="edit" title="SalesItemPriceSync" href="SalesItemPrices(2781117417L)" />
<category term="Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:ItemCode>OX11213001-M61</d:ItemCode>
<d:Price m:type="Edm.Double">136.36</d:Price>
</m:properties>
</content>
</entry>
<entry>
<id>https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117419L)</id>
<title type="text"></title>
<updated>2021-08-11T11:47:22Z</updated>
<author>
<name />
</author>
<link rel="edit" title="SalesItemPriceSync" href="SalesItemPrices(2781117419L)" />
<category term="Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:ItemCode>EK1117005.58</d:ItemCode>
<d:Price m:type="Edm.Double">164.46</d:Price>
</m:properties>
</content>
</entry>
</feed>
I need to get the values : ItemCode & Price for each entry.
I tried to do so like this : $data = simplexml_load_string($xml), but when I do print_r($data->entry), here is what I get:
SimpleXMLElement Object
(
[id] => https://start.exactonline.be/api/v1/393999/sync/logistics/SalesItemPrices(2781117417L)
[title] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => text
)
)
[updated] => 2021-08-11T13:47:01Z
[author] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
)
)
[link] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => edit
[title] => SalesItemPriceSync
[href] => SalesItemPrices(2781117417L)
)
)
[category] => SimpleXMLElement Object
(
[@attributes] => Array
(
[term] => Exact.Web.Api.Models.Sync.Logistics.SalesItemPriceSync
[scheme] => http://schemas.microsoft.com/ado/2007/08/dataservices/scheme
)
)
[content] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => application/xml
)
)
)
But I expected to have : <d:ItemCode>OX11213001-M61</d:ItemCode> <d:Price m:type="Edm.Double">136.36</d:Price> in the "content" part.
Then, how can I get these values? and also how to correctly make a loop between these all "entry"?
I tried foreach ($products->entry as $product) but received error : Warning: Invalid argument supplied for foreach()
Upvotes: 1
Views: 78
Reputation: 5335
The following seems to work. Please note that your elements are namespace-d and have a prefix.
$simpleXML = new SimpleXMLElement($xml);
$myResult = [];
foreach($simpleXML->entry as $entry) {
$properties = $entry->content->children('m', true);
$myResult[] = ['itemCode' => (string) $properties->children('d', true)->ItemCode, 'itemPrice' => (string) $properties->children('d', true)->Price];
}
print_r($myResult); // to see the values
Upvotes: 1