0

I am submitting the contents of an HTML form to a 3rd-party service using cURL, and it shoots me back an XML response. But no matter what I'm doing I can't seem to parse that XML response to customize the display of the results.

Here is the code for processing the form (note a few ID numbers have been censored):

<?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>';
}
?>

This is the raw output of the response I get back:

<?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>

When I try using methods like SimpleXmlElement or simplexml_load_string() to parse the XML, they seem to be ignored and I can't get rid of the raw XML output unless I remove the curl_exec($c) line.

Is there something I am doing wrong?

5
  • Have you tried assigning the output: $response = curl_exec( $c );? How were you planning to parse it? Commented Sep 27, 2013 at 21:31
  • Yes, I've tried doing exactly that, and then doing print_r ($response); but it gives me no result. Commented Sep 27, 2013 at 21:32
  • @Evster What's the result of var_dump($response);? Commented Sep 27, 2013 at 21:37
  • @Evster Take a look at the page's source code (right-click in your browser, choose "View source code")! Commented Sep 27, 2013 at 21:37
  • OK so print_r ($response); actually gives me a value of 1 when I view the source code. Calling var_dump($response); gives me bool(true) in the source code. But I should note that the source code is still spitting out all of the unwanted XML before displaying this data as noted in my original post. Commented Sep 27, 2013 at 21:45

1 Answer 1

2

You need to add

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

to get the value (string) of the response; otherwise, curl_exec just returns a boolean (success/failure). See http://php.net/manual/en/function.curl-exec.php

Then you replace the line

curl_exec( $c );

with

$response = curl_exec( $c );

And finally you parse the response string with a parser of your choice.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this appears to be the solution I was looking for!
I should also note that I need to eliminate the direct call to curl_exec( $c ); and instead assign it to a variable, otherwise I problems with duplicate results.
Sorry for any confusion Floris. You are correct. What I meant was that I needed to replace the call to curl_exec($c); with $response = curl_exec($c);. If I left both calls in my code it wouldn't work.
I have edited my answer to make the point unambiguous. We can now delete these comments...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.