'file_get_contents('php://input') not working. Empty - NULL

I having this problem for the last 12 hours, i read about 50 articles, 50 questions here, i can't fix it. I will be more precise, i want others get the solution too.

The problem: I have a hosting account in namech****, and a local server. Linux there and Xampp Windows here, this only works in my local server.

test.php:

<?php    
    $data = json_decode(file_get_contents("php://input"), true);
        var_dump($data);
?>

The post request is:

POST /test.php HTTP/1.1
Host: myweb.io
Content-Type: application/json
Content-Length: 77

{
  "test": "product/created",
  "other": "https://google.com/"
}

I use REQBIN for test the POST request.

This is the response in my hosting:

HTTP/1.1 200 OK
Date: Sun, 03 May 2020 17:04:33 GMT
Server: Apache
X-Powered-By: PHP/7.4.5
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

NULL

This is the correct response on my local enviroment using XAMPP

HTTP/1.1 200 OK
Date: Sun, 03 May 2020 17:15:19 GMT
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.3
X-Powered-By: PHP/7.4.3
Content-Length: 94
Content-Type: text/html; charset=UTF-8

Array
(
    [test] => product/created
    [other] => https://google.com/
)

Things i've tried:

  • Set the allow_url_fopen to 1, or 0.
  • Downgrade PHP versión (Tried 7.1,2,3,4)
  • Uninstall SSL Certificate
  • Millions of things in PHP.INI
  • Removing htaccess
  • Removing some php modules / extensions UPDATE:
  • The file is individual, cheched that the function is only execute one time
  • I check if there are no redirections. (Htaccess, cPanel settings)


Solution 1:[1]

I fix it, don't know how, but i think the solution was: Delete my htaccess completely, no redirections (http to https). Dont test with reqbin, test it with php pure code, or PostMan.

Solution 2:[2]

Believe it or not:

Changing: RewriteRule ^ /%1 [NC,L,R]

to

RewriteRule ^ %1 [NC,L,R]

and then back to

RewriteRule ^ /%1 [NC,L,R]

And similarly RewriteRule ^(.*?)/?$ /$1.php [NC,L]

to

RewriteRule ^(.*?)/?$ $1.php [NC,L]

and then back to

RewriteRule ^(.*?)/?$ /$1.php [NC,L]

Appeared to sort it out!

Solution 3:[3]

I also had this problem, and I realized that my address was http:// instead of https://.

Now I have something with file_get_contents("php://input")

I hope this will solve your problem.

Solution 4:[4]

I had the same issue; apparently an HTTP Redirect (e.g 301 to https) was prohibiting the data from being redirected along.

Make sure you have no redirects.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ayrton Scott
Solution 2 Pieter Eksteen
Solution 3 cursorrux
Solution 4 Al-Punk