nasht00
(Yiiframework Com)
August 22, 2011, 2:56pm
1
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.
mdomba
(Maurizio Domba Cerin)
August 23, 2011, 6:58am
2
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
nasht00
(Yiiframework Com)
August 23, 2011, 7:04am
3
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.
mdomba
(Maurizio Domba Cerin)
August 23, 2011, 8:26am
4
Never saw that… can you get the code on how this POST is sent?
Anyway… what you get with
print_r($_POST)
1 Like
Wartex
(Wartex)
August 23, 2011, 6:08pm
5
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
Wartex
(Wartex)
August 23, 2011, 6:14pm
6
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?
nasht00
(Yiiframework Com)
August 23, 2011, 6:20pm
7
Wartex:
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.
patrick_s
(Patrick)
September 23, 2015, 2:34pm
8
is it the way of
$GLOBALS["HTTP_RAW_POST_DATA"]
simpler to get raw http post data ?
akseni
(Kadrovic Ines)
September 29, 2015, 3:02pm
9
Maybe use Yii::app()->request->getRawBody()
2 Likes