Solved

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

Posted on 2011-02-12
7
304 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:fundraisinggold
7 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 34880751
The second one begins <? rather than <?php

If your server does not support SHORT_TAGS then the second script will not execute
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34880768
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);

0
 
LVL 2

Accepted Solution

by:
requeue earned 250 total points
ID: 34881131
Hi,

You have to make $_post  to upper case in test2.php.

<test>
<? print_r($_POST); ?>
</test>

Open in new window


Then you will get output:

<test>
Array
(
    [<?xml_version] => \"1.0\" encoding=\"UTF-8\"?>
<rootNode>
    <innerNode>
    </innerNode>
</rootNode>
)
</test>

Open in new window


0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34882952
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34882972
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

0
 

Author Closing Comment

by:fundraisinggold
ID: 34883269
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34883291
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now