0

Possible Duplicate:
PHP php://input vs $_POST

I'm using the Facebook Real-time updates API to subscribe to changes. The docs state:

Following a successful subscription, Facebook will proceed to call your endpoint every time that there are changes (to the chosen fields or connections). For each update, it will make an HTTP POST request.

The request will have content type of application/json and the body will comprise a JSON-encoded string containing one or more changes.

~ https://developers.facebook.com/docs/reference/api/realtime/

I tried over and over to access the POSTed data use $_POST, which was always empty. After googling for a while, i found this blog post which contained this magical line: $post_body = file_get_contents('php://input');.

I've never seen php://input before.... what is this? what does it do? What is Facebook doing on their side to create what I assume is a file with the JSON string in it? Why would they do this rather than sending it through $_POST?

Community
  • 1
  • 1
Brian Hosie
  • 195
  • 1
  • 1
  • 9

2 Answers2

2

php://input is a stream wrapper over raw input body. It's parsed by php automatically so you could get $_POST in your code. If it doesn't - this means that it is in an unexpected format.

In the case of facebook - they send just a json string which isn't what PHP interpreter expects there to be (it expects it to be a key=val&key2=val&... string, and gets {key: "val",...} instead).

That's why you need to read and parse it manually.

zerkms
  • 249,484
  • 69
  • 436
  • 539
0

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives.

Reference http://php.net/manual/en/wrappers.php.php

admoghal
  • 1,498
  • 1
  • 10
  • 5