homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Browser outputs PHP code
chrisrogers




msg:3840479
 5:44 pm on Feb 2, 2009 (gmt 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]

 

Morgenhund




msg:3840687
 10:12 pm on Feb 2, 2009 (gmt 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!

chrisrogers




msg:3840789
 1:29 am on Feb 3, 2009 (gmt 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]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved