Link to home
Start Free TrialLog in
Avatar of JeffreyLevesque
JeffreyLevesque

asked on

Verifying an XML attribute to determine PHP redirect

OK Experts,

I've been searching through EE for the past 3 days trying to work out a solution on my own, but
it's time to call in the cavalry!

Here's my Process:

1) User fills out a form on a PHP page. (request-quote.php)
2) Form data is passed to a PHP page and data is processed and posted to a lead
service using cURL. (process.php)
3) Lead service posts back a response in XML.
4) The cURL in process.php reads response and prints it.

I got the process to that point working fine.  Here's where I get lost and what I would
"like" to happen.

5) Read a specific attribute and react depending on the response (true,false).

True:  redirect to a URL
False: Display a message

I have no need/desire to have this information stored in a database, so I'd like to avoid
that if possible.

The server is running PHP 5.2.9 and has DOM enabled.

Below is a copy of the XML response that I am receiving from the lead service,
followed by the code I currently have within "process.php"

My arrogance and ego, assuming that I could figure it out on my own before the
weekend was finished, has put me in a tight spot. LOL

I've tried dozens of solutions that I've found here and each one has resulted in an error.






<?xml version="1.0" encoding="utf-8"?>
<PostResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.leadwebsite.com/">
  <isValidPost>true</isValidPost>
  <ResponseType>No_Error</ResponseType>
  <ResponseDetails>Lead was accepted</ResponseDetails>
  <LeadIdentifier>1111111</LeadIdentifier>
  <VendorAccountAssigned>2222</VendorAccountAssigned>
  <PendingQCReview>true</PendingQCReview>
  <Price>0</Price>
</PostResponse>

=====================================================

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://www.leadsite.com/services/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$post_data = array();
foreach ($_POST as $key => $value) {
  $post_data[] .= "$key=$value";
}
$query_string = implode('&',$post_data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string );

$returnValue = curl_exec($ch);
curl_close ($ch);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
</head>

<body>

<?

$data=<<<XML

echo("$returnValue" );

XML;

$doc = new DOMDocument();
$doc->loadXML($data);
$responseNode = $doc->getElementsByTagName("isValidPost")->item(0);
if ($responseNode->nodeValue=='true') {
  header("Location: http://www.blumentals.net");
  exit;
} else {
  echo "Failure";
}
?>

</body>

</html>

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

you are loading an INVALID xml onto the DOMDocument object.
$data=<<<XML
echo("$returnValue" );
XML;

your echo becomes part of the xml string:
echo("<?xml....");

what you need is to extract the value of $returnValue WITHOUT the echo statement:
$data=<<<XML
{$returnValue"}
XML;
Avatar of JeffreyLevesque
JeffreyLevesque

ASKER

OK, I've tried it with:

($returnValue)

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity, line: 2 in /home/loanmodi/public_html/process.php on line 41
Failure

{$returnValue}

Warning: DOMDocument::loadXML() [domdocument.loadxml]: XML declaration allowed only at the start of the document in Entity, line: 2 in /home/loanmodi/public_html/process.php on line 41
Failure

{"$returnValue"}
Warning: DOMDocument::loadXML() [domdocument.loadxml]: XML declaration allowed only at the start of the document in Entity, line: 2 in /home/loanmodi/public_html/process.php on line 41
Failure


{$returnValue"}
Parse error: syntax error, unexpected '"', expecting '}' in /home/loanmodi/public_html/process.php on line 36

$returnValue
Warning: DOMDocument::loadXML() [domdocument.loadxml]: XML declaration allowed only at the start of the document in Entity, line: 2 in /home/loanmodi/public_html/process.php on line 41
Failure
save the attached code as hielo.php. IF it does not work, then look at the browser's source code. What do you get/see?
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.leadsite.com/services/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$post_data = array();
foreach ($_POST as $key => $value) {
  $post_data[] .= "$key=$value";
}
$query_string = implode('&',$post_data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string );

$returnValue = curl_exec($ch);
curl_close ($ch);

echo "<!-- Returned value:\n";
var_dump($returnValue);
echo "\n -->";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
</head>

<body>

<?php
$doc = new DOMDocument();
$doc->loadXML($returnValue);
$responseNode = $doc->getElementsByTagName("isValidPost")->item(0);
if ($responseNode->nodeValue=='true') {
  header("Location: http://www.blumentals.net");
  exit;
}
else
{
	echo "Failure";
}
?>

</body>

</html>

Open in new window

Here's the source code of the error:

<!-- Returned value:
string(499) "<?xml version="1.0" encoding="utf-8"?>
<PostResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.leadproweb.com/">
  <isValidPost>true</isValidPost>
  <ResponseType>No_Error</ResponseType>
  <ResponseDetails>Lead was accepted</ResponseDetails>
  <LeadIdentifier>8888888</LeadIdentifier>
  <VendorAccountAssigned>9999</VendorAccountAssigned>
  <PendingQCReview>true</PendingQCReview>
  <Price>0</Price>
</PostResponse>"

 --><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
</head>

<body>

<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/loanmodi/public_html/process.php:20) in <b>/home/loanmodi/public_html/process.php</b> on line <b>40</b><br />

I tried a couple other things and that didn't work either.
I guess if I have to, I can just post a message with a link, but at least it's processing
through the "true" validation now.

I had tried something similar last night, but didn't have the code quite right.

try replacing:
$returnValue = curl_exec($ch);

with:
$returnValue=preg_replace('#^<[\?]xml[^>]+>#', '', curl_exec($ch) );

You might be experiencing the problem described here:
http://us3.php.net/manual/en/domdocument.loadxml.php#94291
Got the same error:

<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/loanmodi/public_html/process.php:20) in <b>/home/loanmodi/public_html/process.php</b> on line <b>40</b><br />

I also tried swapping the loadXML() to loadHTML()  and that was a total melt-down
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just tried something crude.

Instead of using header('Location: http://www.aa.com'); , I moved all the HTML into an
echo function and used META Refresh.

It works in Firefox, but in IE, I'm getting:

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

The server did not understand the request, or the request was invalid. Error processing resource 'http://www.w3.org/TR/html...
 
Even though everything is showing up fine in the source code.  I guess it's the META Refresh


<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.leadproweb.com/services/interfaces/public/Leadimport.asmx/LeadReceiver");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$post_data = array();
foreach ($_POST as $key => $value) {
  $post_data[] .= "$key=$value";
}
$query_string = implode('&',$post_data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string );

$returnValue = curl_exec($ch);
curl_close ($ch);

echo "<!-- Returned value:\n";
var_dump($returnValue);
echo "\n -->";
?>


<?php
$doc = new DOMDocument();
$doc->loadXML($returnValue);
$responseNode = $doc->getElementsByTagName("isValidPost")->item(0);
if ($responseNode->nodeValue=='true') {
  echo("
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>

<html xmlns='http://www.w3.org/1999/xhtml'>

<head>
  <title>Hello!</title>
  <meta http-equiv='refresh' content='0; url=http://www.blumentals.net'>
</head>

<body>
&nbsp;
</body>

</html>
  ");
  exit;
}
else
{
        echo "Failure";
}
?>

Open in new window

You probably missed my previous post. Give that a try first.
WAHOOO!

Yes, I did miss your post and it works perfectly!  Thank you so much!

I will actually be able to get some sleep tonight.  This issue has been
eating me alive for the past three days.
>>I will actually be able to get some sleep tonight.
Glad to help. I know that feeling of relief oh too well.

In summary, whenever you need to redirect to some other page, you need to do so BEFORE you send any output to the browser, including white space.

Regards,
Hielo
See? I come back from lunch, and not only do you have your solution, hielo's solution is actually more elegant than mine was!