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.

4
  • 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 Commented Dec 27, 2019 at 6:28
  • 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. Commented Dec 27, 2019 at 6:45
  • Have you checked stackoverflow.com/questions/813487/… 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). Commented Dec 27, 2019 at 6:59
  • 1
    PHP only parses Content-Type: multipart/form-data post requests into $_POST, as this content type is commonly used for forms. Commented Dec 27, 2019 at 7:44

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.