duai5344 2017-03-13 04:52
浏览 114
已采纳

Zoho CRM API simpleXMLelement获取多个产品ID

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.

  • 写回答

1条回答 默认 最新

  • drci47425 2017-03-14 10:34
    关注

    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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突