Reputation: 83
This is a sample of the XML returned from ZoHo CRM API. I need to parse out all of the data to insert into a database. Those records that are "Parents" do not have a Parent Account ID or Parent Account Name (see example row 2). This causes Undefined offset: errors at those lines. I am at a loss as to why...
<?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>
I can access all of the desired nodes using 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"]');
Then iterating through...
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/>";
}
I have tried inserting this test in the loop:
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 = "";
}
Which does fine until it hits record 122/157 (shown as row 2 in the sample), then the Undefined Offset errors creep back in.
Updated... to look at this from a DOMDocument perspective.
$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;
}
No data returned.
Upvotes: 1
Views: 1300
Reputation: 21
I know this is old now but there is a parameter you can send which should output all fields whether they're NULL or not, I generally ensure I'm passing :
&newFormat=2&selectColumns=All&version=2
Also I hope the following might help someone, it took me a while to figure out how to get the Zoho data back as a simple array :
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());
The original poster could skip the first 20 lines or so and just load the XML from file.
Upvotes: 2