Symfony 2 获取 json 请求 [英] Symfony 2 get json request

查看:28
本文介绍了Symfony 2 获取 json 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个端点,它为我的应用程序的订阅者接收来自服务的更新(使用第 3 方 api).这些将作为邮件发送给我.我不知道如何获取我尝试过的内容).

I have an endpoint which is receiving updates from a service for my subscribers to my app (using a 3rd party api). These are getting sent to me as a post. I don't know how to get the content I've tried few things).

这是收到的每个文件都有唯一的生成编号

this is what's being received each files has unique generated number

上传的文件更新:update1389874969000.json (application/json)

我试过的这段代码 -

          $request = $this->getRequest(); 

          $request->request->get('updates');

//不报错但不获取任何数据或

// don't get an error but don't get any data or

 $request = $this->getRequest(); 

 $request->files->get('updates');
get '/tmp/php0oJ2oP' which doesn't mean anything to me 

任何想法如何解析这个json数据,谢谢

Any ideas how to parse this json data, thanks

推荐答案

public function postAction(Request $request)
{   
    $personData = json_decode($request->getContent(),true);

好的.所以你没有收到 json 作为内容.相反,正在上传一个 json 文件.

Ok. So your are not receiving json as content. Instead, a json file is being uploaded.

你得到的/tmp/php0oJ2oP 实际上是你上传文件的路径.您可以使用

The /tmp/php0oJ2oP that you got is actually the path to your uploaded file. You can read and convert to a php array with

    $data = json_decode(file_get_contents('/tmp/php0oJ2oP'),true);

当然,每次上传都会获得不同的 tmp 路径,因此请从请求中提取路径.

Of course you will get a different tmp path for each upload so pull the path from the request.

关于文件上传的更多信息可以在这里找到:http://symfony.com/doc/current/reference/forms/types/file.html

More info on file uploads can be found here: http://symfony.com/doc/current/reference/forms/types/file.html

但您真正需要的只是 tmp 路径.

But all you really need is the tmp path.

这篇关于Symfony 2 获取 json 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆