Retrieve POST raw data

I receive data to my application via POST, however the data does not have a variable name.

The post body contains JSON data.

How can I read that data in my controller? I tried


file_get_contents('php://input');

but it didn’t work.

I read that this method can only be used once - so maybe Yii is already using it.

Your problem is not really clear, maybe if you show some code on how you send (post) that data…

Why would you use file_get_content ? What do you mean with "data does not have a variable name"?

Have you tried to use $_POST or getPost() - http://www.yiiframework.com/doc/api/1.1/CHttpRequest#getPost-detail

It turns out file_get_contents was working, it was another issue.

The code that generates the POST is not mine, I am getting it from an API.

POST requests don’t always have variable names such as “something=test”. In this case the whole body of the POST is one big JSON. Which so far with my research, can only be accessed by file_get_contents(‘php://input’); , unless someone has a more elegant idea.

$_POST only works when you have variable names.

Never saw that… can you get the code on how this POST is sent?

Anyway… what you get with


print_r($_POST)

1 Like

Hi!

mdomba it’s a predifined variable but not the $_POST

http://php.net/manual/en/reserved.variables.httprawpostdata.php

for example lot of flash apps will communicate via raw_post_data with xml… u can read stream from the request body

ooo… sorry i never used it with yii, but u can check it in the core WebService component.

are u sure file_get_contents(‘php://input’); contains data?

Well like I said it’s all good now, I think i had a typo somewhere and it works now.

But it’s something to look into, maybe it can be integrated into the Yii framework.

is it the way of




$GLOBALS["HTTP_RAW_POST_DATA"] 



simpler to get raw http post data ? :rolleyes:

Maybe use Yii::app()->request->getRawBody()

2 Likes