Page 1 of 1

$_POST is always empty

Posted: Sun Oct 31, 2010 8:10 am
by vivek.m
Can someone run the following script on their own server and test?

test.php

Code: Select all

<?php
$url = "http://localhost/postdump.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('"Content-type: text/plain; charset=UTF-8"'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Hello");
$result = curl_exec($ch);
?>
postdump.php

Code: Select all

<?php
echo "Inside post dump--";
//print_r($_SERVER);
//$data = file_get_contents('php://input');
//print_r($data);
print_r(count($_POST));
print_r($_POST);
//print_r(count($_REQUEST));
//print_r($_REQUEST);
?>
I am always getting $_POST as empty.

Re: $_POST is always empty

Posted: Sun Oct 31, 2010 8:23 am
by Eran
You need to pass the POST data as a URL query string or as an array. Just a string without an index will do nothing

Code: Select all

curl_setopt($ch, CURLOPT_POSTFIELDS, "param=Hello");
//Or
curl_setopt($ch, CURLOPT_POSTFIELDS, array('param' => "Hello"));

Re: $_POST is always empty

Posted: Sun Oct 31, 2010 8:42 am
by vivek.m
Eran wrote:You need to pass the POST data as a URL query string or as an array. Just a string without an index will do nothing

Code: Select all

curl_setopt($ch, CURLOPT_POSTFIELDS, "param=Hello");
//Or
curl_setopt($ch, CURLOPT_POSTFIELDS, array('param' => "Hello"));
oh! you are right. After making the said changes, $_POST shows correct data!!! OMG. I have checked 100's of links on google relaed to this problem and none of them mentioned this thing.
Can you please have a look at my question here?
How do I pass a JSON string? Am I doing it incorrectly in the code sample in SO link?

Re: $_POST is always empty

Posted: Sun Oct 31, 2010 8:49 am
by Eran
I answered you there. It's the same problem - you aren't using any parameter index