Link to home
Start Free TrialLog in
Avatar of fundraisinggold
fundraisinggold

asked on

Why do I get a blank response when I send a post request to a url?

I have been struggling mightily over what is probably a simple question.  The web is filled with Curl examples that show how to post something to another script and get a response.  I've tried lots of variations and can't get anything back on the response.  The response does not include any data and does not include any curl error information.  I've tried to boil this down to a very simple scenario which I've demonstrated in the two scripts below.  Both scripts run on the same server.  The first initiates a post request to send a simple xml string and the second is simply echoing something back.

test1.php
<?php

$post_string = '<?xml version="1.0" encoding="UTF-8"?>
<rootNode>
    <innerNode>
    </innerNode>
</rootNode>';

$url = "http://bsmsoftware.us/test2.php";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //Don't verify ssl certificate
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$reply = curl_exec($ch);

if ($reply!== false) echo $reply; else echo curl_error($ch);

?>

Open in new window


test2.php
<test>
<? print_r($_post); ?>
</test>

Open in new window


Like I said, I've been struggling with this for awhile, so I'm looking forward to some help.
SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland 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
Another point - the text to be returned is in < > delimiters and the browser will treat it as markup so it may be invisible when rendered. So instead of

if ($reply!== false) echo $reply; else echo curl_error($ch);

use

if ($reply!== false) echo htmlentities( $reply ); else echo curl_error($ch);

ASKER CERTIFIED SOLUTION
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
Let's look at just test2.php and break down this one, apparently simple, line of code.  It comes with a lot of assumptions and those assumptions are NOT what you want when you are debugging.

<? print_r($_post); ?>

The first assumption is that short-tags are enabled and that you actually want to use short tags.  You do not actually want to use short tags.  Here is why.  Consider these two lines:

<? // DOES THIS LINE START PHP OR DOES IT START XML?
<?xml

Can you see how the PHP short tags can be confused with XML tags?  For that reason, without regard to whether short-tags are enabled, you want to make it a habit and coding standard that you practice: Always use the full <?php open tag.
http://www.php.net/manual/en/language.basic-syntax.phpmode.php

The next assumption is that $_post is the correct variable name, that it is defined, not empty, and can be printed by print_r().  None of these statements are true.  The correct variable name is $_POST.  The reason all of this escaped your test is that the default error reporting level for PHP suppresses Notice messages, and an undefined variable is not an error in PHP -- it only rises to the level of a Notice.  You can change this behavior by setting error_reporting(E_ALL).  And when you are trying to debug software, you want to see all the errors.

The last assumption that seems to have problematic potential is the assumption that the POST method will actually return anything within the time limit.  It might not.  POST is a method that allows the target script to run asynchronously.  It is perfectly acceptable to start a script with POST and never wait for it to complete.

Here is how I would re-write test2.php for the initial tests. Once you got that to work, then you could go back and start adding in the XML tags.

HTH, ~Ray
<?php // test2.php
// SET THE RIGHT ERROR REPORTING FOR DEVELOPMENT AND DEBUGGING
error_reporting(E_ALL);
// START THE OUTPUT BUFFERS 
ob_start();
// GUARANTEE SOME OUTPUT - DELETE THIS AFTER THE FIRST SUCCESSFUL TEST
echo "HELLO WORLD";
// DUMP THE POST ARRAY INTO THE OUTPUT BUFFER
print_r($_POST);
// COLLECT THE BUFFER AND ECHO IT INTO THE BROWSER OUTPUT STREAM
$ob = ob_get_clean();
echo $ob;
die();

Open in new window

Here is another way of looking at test2.php, once you have ensured that you can get data back and forth with CURL. Untested code but valid in principle, I think...

When you get this back, use view source or use htmlentities() to visualize the XML string.
<?php // test2.php
// SET THE RIGHT ERROR REPORTING FOR DEVELOPMENT AND DEBUGGING
error_reporting(E_ALL);
// START THE OUTPUT BUFFERS 
ob_start();

// CONVERT THE POST TO AN XML STRING
echo '<?xml>' . PHP_EOL;
echo '<post>' . PHP_EOL;
foreach ($_POST as $key => $val)
{
    echo "<$key>$value</$key>" . PHP_EOL;
}
echo '</post>' . PHP_EOL;

// BROWSER OUTPUT IS FLUSHED AT SCRIPT END
die( PHP_EOL );

Open in new window

Avatar of fundraisinggold
fundraisinggold

ASKER

Thanks to everyone.  I know that $_post doesn't equal $_POST.  I guess I was getting so caught up in how to format the CURL options and whether to use headers or not, that I didn't pay much attention to test2.php.
Just a thought for going forward.  If you use $_REQUEST instead of $_POST, you can test by adding arguments to your URL string.  $_REQUEST contains both POST and GET variables.  With a strategy like that you would have been able to verify that test2.php was working before you relied on it to test your other script.