Welcome to WebmasterWorld Guest from 54.204.198.71

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Browser outputs PHP code

     
5:44 pm on Feb 2, 2009 (gmt 0)

New User

5+ Year Member

joined:Feb 2, 2009
posts:2
votes: 0


I have some PHP code that I am trying to run on my web server. It makes a POST request to a web site via HTTPS and then is supposed to print the response.

Trouble is, instead of printing the response, it prints my PHP code.

I think the trouble is the way my string literal is formed- I need to escape something (it contains a ?> among other things), but I'm not sure how to do it. I tried putting a backslash \ before a few characters in the string but it produced the same response.

Here is the code:


<html>
<body>
<?php

function httpsPost($Url, $strRequest)
{
// Initialisation
$ch=curl_init();
// Set parameters
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_URL, $Url);
// Return a variable instead of posting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Active the POST method
curl_setopt($ch, CURLOPT_POST, 1) ;
// Request
curl_setopt($ch, CURLOPT_POSTFIELDS, $strRequest);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// execute the connexion
$result = curl_exec($ch);
// Close it
curl_close($ch);
return $result;
}

$url = 'https://example.com/wps2/location';
$strRequest =utf8_encode("<?xml version='1.0'?><LocationRQ xmlns='http://example.com/wps/2005' version='2.6' street-address-lookup='full'><authentication version='2.0'><simple><username>beta</username><realm>js.loki.com</realm> </simple></authentication><access-point><mac>00AA11BB22CC</mac><signal-strength>-50</signal-strength></access-point></LocationRQ>");
$Response = httpsPost($url, $strRequest);

echo $Response;

?>

</body>

</html>

And the response (in Firefox and IE):


example.com00AA11BB22CC-50"); $Response = httpsPost($url, $strRequest); echo $Response; ?>

Thanks for any help. :)

[edited by: eelixduppy at 7:59 pm (utc) on Feb. 2, 2009]
[edit reason] exemplified [/edit]

10:12 pm on Feb 2, 2009 (gmt 0)

Full Member

10+ Year Member

joined:May 11, 2004
posts: 278
votes: 0


I suppose script prints out everything correctly, but XML does not show up in browser (because browser recognizes XML as markup, not data to show).

To make sure, look at the page source when getting response.

Or alternatively, replace "$strRequest =utf8_encode" with
$strRequest = htmlspecialchars(utf8_encode" to see the full output in the browser window.

Hope that helps!

1:29 am on Feb 3, 2009 (gmt 0)

New User

5+ Year Member

joined:Feb 2, 2009
posts: 2
votes: 0


Nope, still does that.

When I view the source of the page, this is what I get:


<html>
<body>
<?php

function httpsPost($Url, $strRequest)
{
// Initialisation
$ch=curl_init();
// Set parameters
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_URL, $Url);
// Return a variable instead of posting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Active the POST method
curl_setopt($ch, CURLOPT_POST, 1) ;
// Request
curl_setopt($ch, CURLOPT_POSTFIELDS, $strRequest);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// execute the connexion
$result = curl_exec($ch);
// Close it
curl_close($ch);
return $result;
}

$url = 'https://example.com/wps2/location';
$strRequest = htmlspecialchars( utf8_encode("<?xml version='1.0'?><LocationRQ xmlns='http://example.com/wps/2005' version='2.6' street-address-lookup='full'><authentication version='2.0'><simple><username>beta</username><realm>example.com</realm></simple></authentication><access-point><mac>00AA11BB22CC</mac><signal-strength>-50</signal-strength></access-point></LocationRQ>") );
$Response = httpsPost($url, $strRequest);

echo $Response;

?>

</body>

</html>

What's interesting to note is that the whole <?php section appears in pink (using Firefox) until I get to <LocationRQ xmlns='http://example.com/wps/2005'...

Then it is colored as HTML. So maybe FF thinks the XML code is HTML?

Apparantly my server isn't parsing it as PHP.

[edited by: eelixduppy at 2:10 am (utc) on Feb. 3, 2009]
[edit reason] example.com [/edit]