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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?