4

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)
Amalgama29234
  • 63
  • 1
  • 1
  • 6
  • 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? – Álvaro González May 03 '20 at 17:21
  • Please have a look at this: https://stackoverflow.com/questions/18866571/receive-json-post-with-php – Salim Ibrohimi May 03 '20 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. – Álvaro González May 03 '20 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" – Salim Ibrohimi May 03 '20 at 17:27
  • OP HERE: @ÁlvaroGonzález var_dump(file_get_contents("php://input")); output: string(0) "" – Amalgama29234 May 03 '20 at 17:31
  • OP HERE: @SalimIbrogimov i think its a valid json data, isn't? – Amalgama29234 May 03 '20 at 17:33
  • 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? – iainn May 03 '20 at 17:36
  • `Content-Length: 77` looks like too much (even with Windows line feeds it should be around 70). Do you really have that much data (e.g. empty lines or trailing spaces)? – Álvaro González May 03 '20 at 17:37
  • @ÁlvaroGonzález sorry, i paste the lenght from other . – Amalgama29234 May 03 '20 at 17:38
  • @iainn i think i have no redirects. No htaccess, no ssl. No redirects in cPanel. – Amalgama29234 May 03 '20 at 17:39
  • One more silly test: does it get through if you replace `https://google.com` with anything else, e.g. `kittens`? – Álvaro González May 03 '20 at 17:52
  • @ÁlvaroGonzález yes its working, it output the whole google site. – Amalgama29234 May 03 '20 at 18:26
  • I mean that, in your request, remove `https://google.com` and type a simple text that isn't a URL. That's to discard there's a security filter removing requests that contain URLs. Also, an additional diagnostic you can do is to upload a regular HTML form to your site and submit from there; that's to figure out if HTTP referrer matters. – Álvaro González May 04 '20 at 06:32

4 Answers4

2

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.

Al-Punk
  • 3,531
  • 6
  • 38
  • 56
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!

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.

Amalgama29234
  • 63
  • 1
  • 1
  • 6
-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.

cursorrux
  • 1,382
  • 4
  • 9
  • 20
Jej
  • 1