这是ZoHo CRM API返回的XML示例。我需要解析出所有要插入数据库的数据。那些"父"记录没有父帐户ID或父帐户名称(请参见示例第2行)。这会导致未定义的偏移:这些行出现错误。我不知道为什么。。。
<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/crm/private/xml/Accounts/getRecords">
<Accounts>
<row no="1">
<FL val="ACCOUNTID">123456789</FL>
<FL val="Account Name"><![CDATA[My Account Center]]></FL>
<FL val="PARENTACCOUNTID">234567891</FL>
<FL val="Parent Account"><![CDATA[Global Corp]]></FL>
<FL val="Shipping State"><![CDATA[IN]]></FL>
<FL val="Account Status"><![CDATA[Active Account]]></FL>
</row>
<row no="2">
<FL val="ACCOUNTID">234567891</FL>
<FL val="Account Name"><![CDATA[Global Corp]]></FL>
<FL val="Shipping State"><![CDATA[IN]]></FL>
<FL val="Account Status"><![CDATA[Active Account]]></FL>
</row>
</Accounts>
</response>
我可以使用XPATH:访问所有需要的节点
$accounts = $XML->xpath('/response/result/Accounts/row/FL[@val="ACCOUNTID"]');
$acctName = $XML->xpath('/response/result/Accounts/row/FL[@val="Account Name"]');
$pAcctID = $XML->xpath('/response/result/Accounts/row/FL[@val="PARENTACCOUNTID"]');
$pAcctName = $XML->xpath('/response/result/Accounts/row/FL[@val="Parent Account"]');
$state = $XML->xpath('/response/result/Accounts/row/FL[@val="Shipping State"]');
然后迭代。。。
for ($i = 0; $i < $itemsTotal; $i++) {
$j = $i + 1;
echo "Counter: " . $i . "<br/>";
echo "Record ID: " . $j . "<br/>";
echo "Account ID: " . $accounts[$i] . "<br/>";
echo "Account Name: " . $acctName[$i] . "<br/>";
echo "Location State: " . $state[$i] . "<br/>";
echo "Parent Account ID: " . $pAcctID[$i] . "<br/>";
echo "Parent Account Name: " . $pAcctName[$i] . "<br/>";
}
我试着在循环中插入这个测试:
if (isset($pAcctID[$i])) {
$pACT = $pAcctID[$i];
$pActName = $pAcctName[$i];
echo "Parent Account ID: $pACT<br/>";
echo "Parent Name: $pActName<br/>";
} else {
echo "Is Parent Account<br/>";
$pACT = "";
$pActName = "";
}
这很好,直到它达到记录122/157(如样本中的第2行所示),然后"未定义偏移"错误再次出现。
已更新。。。从DOMDocument的角度来看。
$doc = new DOMDocument();
$doc->load('zoho.xml');
$doc->saveXML();
$xpath = new DOMXPath($doc);
foreach($xpath->query("//response/results/Accounts/row") as $data){
echo "Account ID is: " . $xpath->query(".//FL[@val='ACCOUNTID']",$data)->item(0)->nodeValue;
}
未返回任何数据。
我知道这是旧的,但有一个参数你可以发送,它应该输出所有字段,无论它们是否为NULL,我通常会确保我通过:
&newFormat=2&selectColumns=All&version=2
此外,我希望以下内容可能会对某人有所帮助,我花了一段时间才弄清楚如何将Zoho数据作为一个简单的数组返回:
public function getCustomerRecordsALL()
{
$token = 'YourTokenHere'; //'$this->token;
$url = "https://crm.zoho.com/crm/private/xml/Accounts/getRecords";
$param= "authtoken=".$token."&scope=crmapi&newFormat=2&selectColumns=All&version=2&toIndex=200&fromIndex=-1&version=2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
// FIX THIS
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
// First deal with problem with Simple XML and CDATA
$sxo = simplexml_load_string($result, null, LIBXML_NOCDATA);
#$sxo = simplexml_load_string(file_get_contents('testdata.txt'), null, LIBXML_NOCDATA); // TESTING ONLY
$data = ($sxo->xpath('/response/result/Accounts/row'));
//Set the variables you're expecting to capture
$arrVariables = array("Account Name", "Address 1", "Address 2", "Address 3", "Email");
$retArr = NULL;
foreach($data as $row) // Iterate over all the results
{
foreach($arrVariables as $arrVar) // Iterate through the variables we're looking for
{
$rowData = ($row->xpath('FL[@val="'.$arrVar.'"]'));
@$arrReturn[$arrVar] = (string)$rowData[0][0];
}
$retArr[] = $arrReturn;
}
return $retArr;
}
print_r(getCustomerRecordsALL());
最初的发布者可以跳过前20行左右,只从文件中加载XML。