I am doing my first steps with WordPress_plugins/PHP and just faced my first wall. Will try to do my best in order to explain the problem, please give me some discount.
I am creating a plugin for WordPress website that needs to call an external Rest API server and get the inventory value of a specific product, for this purpose I am using wp_remote_get & wp_remote_retrieve_body. Until this point everything is working fine, the connection is made successfully and I do get the response in XML.
The problem starts when I try to extract the data (value of the quantity in stock) from the XML response. For some reason it is resulting in NULL, I have printed the response just to make sure the data is actually there and indeed it is.
Also, I stored the printed response in to a variable and tried to use the same logic in order to get the value I am looking for and it worked just fine.
Please see my code/comments below, any advice will be much appreciated, thank you:
//The Rest API xml response obtained by wp_remote_get & wp_remote_retrieve_body.
$response_body = get_api_response($api_url, $arguments);
//The xml data for the "$response_body_manual" was obtained by "Copy Outer HTML" when inspecting the printed/echo element of the "$response_body" on the web browser
$response_body_manual =
'<!--?xml version="1.0" encoding="UTF-8"?-->
<inventories>
<inventory>
<product>TestProduct</product>
<productinventories>
<productinventory>
<site>101</site>
<quantityinstock>238.00000</quantityinstock>
<quantityhardallocated>0.00000</quantityhardallocated>
<quantitysoftallocated>0.00000</quantitysoftallocated>
</productinventory>
</productinventories>
</inventory>
</inventories>';
//Parse the response
$xml = simplexml_load_string($response_body);
$xml_manual = simplexml_load_string($response_body_manual);
//Get the quantityinstock value
$QtyInStock_xml = $xml->inventory->productinventories->productinventory->quantityinstock;
$QtyInStock_xml_manual = $xml_manual->inventory->productinventories->productinventory->quantityinstock;
//Print out the results
echo "<center><pre>";
print($response_body . "<br>");
print("XML String loaded successfully" . "<br>");
print("Quantity_in_stock_xml -> " . $QtyInStock_xml . "<br>");
print("Quantity_in_stock_xml_manual -> " . $QtyInStock_xml_manual . "<br>");
echo "</center></pre>";
//Result
//
//<pre>
// <!--?xml version="1.0" encoding="UTF-8"?-->
// <inventories>
// <inventory>
// <product>TestProduct</product>
// <productinventories>
// <productinventory>
// <site>101</site>
// <quantityinstock>238.00000</quantityinstock>
// <quantityhardallocated>0.00000</quantityhardallocated>
// <quantitysoftallocated>0.00000</quantitysoftallocated>
// </productinventory>
// </productinventories>
// </inventory>
// </inventories>
//
//<br>XML String loaded successfully
//<br>Quantity_in_stock_xml ->
//<br>Quantity_in_stock_xml_manual -> 238.00000<br></pre>
As you can see, I am able to get the quantity in stock value when using a manually created response but not when directly from the api response variable.
Question: What am I missing/doing wrong when getting the XML element value directly from the API response variable?
Thank you