0

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.

E_net4
  • 27,810
  • 13
  • 101
  • 139
rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • PHP has been a while for me but why would you want to post form fields and declare your content as json? Because it isn't. `curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('!!THIS_IS_GREAT!!' => 'VERY VERY GREAT!!!')));` If you want it to be json just format it as json and put it in the body of your post. – Mathijs Segers Dec 27 '19 at 06:45
  • Have you checked https://stackoverflow.com/questions/813487/how-to-post-json-to-php-with-curl/813510#813510 which says I believe you are getting an empty array because PHP is expecting the posted data to be in a Querystring format (key=value&key1=value1). – Amanjot Kaur Dec 27 '19 at 06:59
  • 1
    PHP only parses `Content-Type: multipart/form-data` post requests into `$_POST`, as this content type is commonly used for forms. – arkuuu Dec 27 '19 at 07:44
  • my friend you most be parse array your warning is here you can use foreach loop to parse array or echo $_POST ['test'] your print have array most be parse – AliReza Azizi Dec 27 '19 at 06:28

1 Answers1

1

None of the superglobals are documented to parse JSON. Particularly, $_POST is described as:

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

You can inspect raw POST data with the php://input stream wrapper (e.g. using file_get_contents()) and you can decode JSON with json_decode().

Álvaro González
  • 142,137
  • 41
  • 261
  • 360