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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?