PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
vivek.m
Forum Newbie
Posts: 2 Joined: Sun Oct 31, 2010 8:05 am
Post
by vivek.m » Sun Oct 31, 2010 8:10 am
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.
Eran
DevNet Master
Posts: 3549 Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME
Post
by Eran » Sun Oct 31, 2010 8:23 am
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"));
vivek.m
Forum Newbie
Posts: 2 Joined: Sun Oct 31, 2010 8:05 am
Post
by vivek.m » Sun Oct 31, 2010 8:42 am
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?
Eran
DevNet Master
Posts: 3549 Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME
Post
by Eran » Sun Oct 31, 2010 8:49 am
I answered you there. It's the same problem - you aren't using any parameter index