I am working with the Zoho CRM api and I was able to extract the product ID when only inserting one product, but can't figure out how to do it with multiple products. https://www.zoho.com/crm/help/api/insertrecords.html#Insert_Multiple_records
I convert the response to simpleXMLElement and I can get the first product ID easily with:
...curl stuff
$data = curl_exec($ch);
$xml = new SimpleXMLElement($data);
$product_id = $xml->result->recorddetail->FL[0];
The question is if I have multiple product ID's sent back how would I get each one in a loop as my code will only return the first product ID successfully. This is an example of the response from 2 products inserted in the api and the returned response:
SimpleXMLElement Object ( [@attributes] => Array ( [uri] =>
/crm/private/xml/Products/insertRecords ) [result] => SimpleXMLElement
Object ( [message] => Record(s) added successfully [recorddetail] => Array (
[0] => SimpleXMLElement Object ( [FL] => Array ( [0] => **2389399000000122065**
[1] => 2017-03-12 21:33:50 [2] => 2017-03-12 21:33:50 [3] =>
SimpleXMLElement Object ( [@attributes] => Array ( [val] => Created By ) )
[4] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => Modified
By ) ) ) ) [1] => SimpleXMLElement Object ( [FL] => Array ( [0] =>
**2389399000000122066** [1] => 2017-03-12 21:33:50 [2] => 2017-03-12 21:33:50
[3] => SimpleXMLElement Object ( [@attributes] => Array ( [val] => Created
By ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [val] =>
Modified By ) ) ) ) ) ) )
Not sure if it shows up in bold but the two values enclosed in ** ** is what I am looking to extract.
The key to this is to understand that this:
$xml->result->recorddetail->FL[0];
Is just shorthand for this:
$xml->result[0]->recorddetail[0]->FL[0];
That should make it obvious that to access the 2nd recorddetail
(with index 1
), you could write this:
$xml->result->recorddetail[1]->FL[0];
Because of the magic SimpleXML provides you can also find out how many there are:
count($xml->result->recorddetail);
And most relevantly for your case, loop over them:
foreach ( $xml->result->recorddetail as $recorddetail ) {
$product_id = $recorddetail->FL[0];
}
As a final tip, you probably want the $product_id
variable to hold an ordinary string, not a SimpleXML object; you get that with a "string cast", like this:
$product_id = (string)$recorddetail->FL[0];