无法解析curl_exec的XML响应

时间:2013-09-27 21:27:48

标签: php xml curl

我正在使用cURL将HTML表单的内容提交给第三方服务,它会向我回复XML响应。但无论我在做什么,我似乎都无法解析XML响应以自定义结果的显示。

以下是处理表单的代码(请注意一些ID号已被审查):

<?php
 $FirstName = $_POST['FirstName'] ;
 $LastName = $_POST['LastName'] ;
 $Zip = $_POST['Zip'] ;
 $EmailAddress = $_POST['EmailAddress'] ;
 $PrimaryPhoneNumber = $_POST['PrimaryPhoneNumber'] ;
 $DateofBirth = $_POST['DateofBirth'] ;



$myvars = '&VID=' . '****' . '&LID=' . '****' . '&AID=' . '****' . '&FirstName=' . $FirstName . '&LastName=' . $LastName .
       '&EmailAddress=' . $EmailAddress . '&PrimaryPhoneNumber=' . $PrimaryPhoneNumber . '&Zip=' . $Zip . '&DateofBirth=' . $DateofBirth;


if ($FirstName!='' && $LastName!='' && $Zip!='' && $EmailAddress!='' && $PrimaryPhoneNumber!='' && $DateofBirth!='') {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'http://secure.leadexec.net/leadimport.asmx/LeadReceiver');
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $myvars);
    curl_exec ($c);
    curl_close ($c);

} else {
    echo '<p>Please make sure you have filled out the form completely</p>';
}
?>

这是我回复的回复的原始输出:

<?xml version="1.0" encoding="utf-8"?>
<PostResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.leadproweb.com/">
  <isValidPost>false</isValidPost>
  <ResponseType>Duplicate_Lead</ResponseType>
  <ResponseDetails>Duplicate Lead, Last Received On: 9/27/2013 2:17:26 PM</ResponseDetails>
  <LeadIdentifier>20889333</LeadIdentifier>
  <VendorAccountAssigned>0</VendorAccountAssigned>
  <PendingQCReview>false</PendingQCReview>
  <Price>0</Price>
  <RedirectURL />
</PostResponse>

当我尝试使用 SimpleXmlElement simplexml_load_string()等方法来解析XML时,它们似乎被忽略了,我无法摆脱原始XML输出除非我删除 curl_exec($ c)行。

我做错了吗?

1 个答案:

答案 0 :(得分:2)

您需要添加

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

获取响应的值(字符串);否则,curl_exec只返回一个布尔值(成功/失败)。见http://php.net/manual/en/function.curl-exec.php

然后你替换

curl_exec( $c );

$response = curl_exec( $c );

最后,您使用您选择的解析器解析响应字符串。