5

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)
13
  • 2
    If you do several things at the same time, you can't know what's failing. What does var_dump(file_get_contents("php://input")); print? Commented May 3, 2020 at 17:21
  • Please have a look at this: stackoverflow.com/questions/18866571/receive-json-post-with-php Commented May 3, 2020 at 17:23
  • I'd also recommend to refrain from trying random stuff you find in Google unless they include a sensible explanation that links it to your problem. I understand your frustration, but these random changes can easily break things. Commented May 3, 2020 at 17:24
  • "It is worth pointing out that if you use json_decode(file_get_contents("php://input")) (as others have mentioned), this will fail if the string is not valid JSON." - @xtrasimplicity And the response from your server returns gzipped data: "Content-Encoding: gzip" Commented May 3, 2020 at 17:27
  • 1
    A common reason you'd be missing POST data due to an environmental issue would be an HTTP redirect - are you sure that's not happening on your hosting? Commented May 3, 2020 at 17:36

5 Answers 5

6

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.

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

Comments

1

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!

Comments

0

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.

Comments

0

this is just happened to my clients website.

My client used Nginx and redirect if its contain www. eg:

https://www.example.com

to

https://example.com

Mycode:

$notifications = json_decode(file_get_contents("php://input"),TRUE);

If Its hitted by www. The result of $notifications is NULL

but if hits by using second link (without www)

The $notifications is contains exactly the inputed.

The solution is: Change the access URL. In my case, change webhook URL.

Comments

-1

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.

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.