Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Verifying an XML attribute to determine PHP redirect

Posted on 2009-12-20
12
Medium Priority
?
531 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:JeffreyLevesque
  • 6
  • 5
12 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 26091203
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;
0
 

Author Comment

by:JeffreyLevesque
ID: 26091455
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
0
 
LVL 82

Expert Comment

by:hielo
ID: 26091864
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JeffreyLevesque
ID: 26091974
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.

0
 
LVL 82

Expert Comment

by:hielo
ID: 26092162
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
0
 

Author Comment

by:JeffreyLevesque
ID: 26092230
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
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 26092269
>>Cannot modify header information - headers already sent by
That warning is because within the if clause you have:
header(...);

BUT you have already sent output to the browser:
<?DOCTYPE...>...

Once you have sent data/output to the browser, you cannot use the header() function. Hence the warning. So, I believe it is actually working. Try this:
<?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);
$returnValue=preg_replace('#^<[\?]xml[^>]+>#', '', curl_exec($ch) );
curl_close ($ch);

$msg="";

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

?>
<!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
echo $msg;
?>

</body>

</html>

Open in new window

0
 

Author Comment

by:JeffreyLevesque
ID: 26092270
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

0
 
LVL 82

Expert Comment

by:hielo
ID: 26092277
You probably missed my previous post. Give that a try first.
0
 

Author Comment

by:JeffreyLevesque
ID: 26092295
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.
0
 
LVL 82

Expert Comment

by:hielo
ID: 26092302
>>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
0
 
LVL 32

Expert Comment

by:DrDamnit
ID: 26092451
See? I come back from lunch, and not only do you have your solution, hielo's solution is actually more elegant than mine was!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question