【问题标题】:Parsing XML Returned By ZoHo CRM API解析 ZoHo CRM API 返回的 XML
【发布时间】:2015-12-29 03:58:48
【问题描述】:

这是从 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 行)之前,这一切正常,然后 Undefined Offset 错误又重新出现。

更新...从 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;
}

没有返回数据。

【问题讨论】:

  • 您的 $pAcctID$pAcctName 数组包含的项比其他数组少,并且索引不会与您的其他变量对齐。也许最好像这样获取所有行:$rows = $XML-&gt;xpath('/response/result/Accounts/row');,然后遍历$itemsTotal,并且在每次迭代中,使用行中的相对 xpath 查询来获取帐户 ID、名称、父级和状态等。
  • 谢谢,基思!我已经编辑了原件以更新我的发现。
  • 在您发布的 XML 中,Accountsresponse 的子代,但是在您的 xpath 中,您正在搜索 results 子代,也许这就是为什么没有返回数据的原因?
  • 如果您将foreach 查询中的表达式更改为$xpath-&gt;query("//response/Accounts/row") as $data,那么它看起来应该可以工作:)

标签: php xml xpath zoho


【解决方案1】:

我知道这已经过时了,但是你可以发送一个参数,它应该输出所有字段,无论它们是否为 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。

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 1970-01-01
    • 2018-12-29
    • 2023-03-03
    • 2019-05-25
    • 2019-11-04
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多