I have a file on a remote server (www.oneofmysites.com/hitme.php) that has:
<?php
$d = array(
'test' => 1,
'request' => $_REQUEST,
'post' => $_POST,
'get' => $_GET,
'server' => $_SERVER,
'session' => $_SESSION,
);
print '<pre>';
print_r($d);
In other words, it just prints out a bunch of variables. I use this to test a post I send there. Which I do as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('!!THIS_IS_GREAT!!' => 'VERY VERY GREAT!!!')));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$server_output = curl_exec($ch);
curl_close ($ch);
print $server_output;
If I run the code like this, with the "application/json" line commented out, then it returns values:
Array
(
[test] => 1
[request] => Array
(
[!!THIS_IS_GREAT!!] => VERY VERY GREAT!!!
)
[post] => Array
(
[!!THIS_IS_GREAT!!] => VERY VERY GREAT!!!
)
[get] => Array
(
)
But the moment I add this line:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
Then the post returns:
Array
(
[test] => 1
[request] => Array
(
)
[post] => Array
(
)
[get] => Array
(
)
What am I doing wrong that would cause these values to not go through? Must I somehow enable json posting specifically? (I doubt it, because I have tried this on 2 different servers).
What am I missing?
UPDATE
I figured it out myself that I had to use this:
echo '<pre>'.print_r(json_decode(file_get_contents("php://input")),1).'</pre>';
It doesn't make sense to me that this doesn't work:
$_POST
But this does:
file_get_contents("php://input")
If anyone knows why, please share.