Zoho CRM API simpleXMLelement 获取多个产品 ID
Zoho CRM API simpleXMLelement get multiple product IDs
我正在使用 Zoho CRM api,我能够在仅插入一个产品时提取产品 ID,但无法弄清楚如何对多个产品执行此操作。 https://www.zoho.com/crm/help/api/insertrecords.html#Insert_Multiple_records
我将响应转换为 simpleXMLElement,我可以通过以下方式轻松获得第一个产品 ID:
...curl stuff
$data = curl_exec($ch);
$xml = new SimpleXMLElement($data);
$product_id = $xml->result->recorddetail->FL[0];
问题是,如果我有多个产品 ID 发回,我将如何在循环中获取每个产品 ID,因为我的代码只会 return 第一个产品 ID 成功。这是 api 中插入的 2 个产品的响应示例和 returned 响应:
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 ) ) ) ) ) ) )
不确定它是否以粗体显示,但 ** ** 中包含的两个值是我要提取的内容。
关键是要理解这个:
$xml->result->recorddetail->FL[0];
只是 shorthand 这个:
$xml->result[0]->recorddetail[0]->FL[0];
这应该很明显,要访问第二个 recorddetail
(索引为 1
),您可以这样写:
$xml->result->recorddetail[1]->FL[0];
由于 SimpleXML 提供的神奇功能,您还可以找出有多少个:
count($xml->result->recorddetail);
与您的案例最相关的是,遍历它们:
foreach ( $xml->result->recorddetail as $recorddetail ) {
$product_id = $recorddetail->FL[0];
}
作为最后的提示,您可能希望 $product_id
变量保存普通字符串,而不是 SimpleXML 对象;你用 "string cast" 得到它,像这样:
$product_id = (string)$recorddetail->FL[0];
我正在使用 Zoho CRM api,我能够在仅插入一个产品时提取产品 ID,但无法弄清楚如何对多个产品执行此操作。 https://www.zoho.com/crm/help/api/insertrecords.html#Insert_Multiple_records
我将响应转换为 simpleXMLElement,我可以通过以下方式轻松获得第一个产品 ID:
...curl stuff
$data = curl_exec($ch);
$xml = new SimpleXMLElement($data);
$product_id = $xml->result->recorddetail->FL[0];
问题是,如果我有多个产品 ID 发回,我将如何在循环中获取每个产品 ID,因为我的代码只会 return 第一个产品 ID 成功。这是 api 中插入的 2 个产品的响应示例和 returned 响应:
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 ) ) ) ) ) ) )
不确定它是否以粗体显示,但 ** ** 中包含的两个值是我要提取的内容。
关键是要理解这个:
$xml->result->recorddetail->FL[0];
只是 shorthand 这个:
$xml->result[0]->recorddetail[0]->FL[0];
这应该很明显,要访问第二个 recorddetail
(索引为 1
),您可以这样写:
$xml->result->recorddetail[1]->FL[0];
由于 SimpleXML 提供的神奇功能,您还可以找出有多少个:
count($xml->result->recorddetail);
与您的案例最相关的是,遍历它们:
foreach ( $xml->result->recorddetail as $recorddetail ) {
$product_id = $recorddetail->FL[0];
}
作为最后的提示,您可能希望 $product_id
变量保存普通字符串,而不是 SimpleXML 对象;你用 "string cast" 得到它,像这样:
$product_id = (string)$recorddetail->FL[0];