PHP 使用 JSON 流
我有一个客户端/服务器应用程序,其中客户端以 JSON 形式将对象发送到运行 PHP 脚本的服务器,然后将此数据放入数据库中。
问题是解码是使用 json_decode 函数完成的,该函数似乎适用于字符串而不是流。有没有办法从 HTTP 请求中获取输入流并使用流式 JSON 解析器来减少内存占用。
我有java背景,有几个框架,比如 jackson 、 xtream 。有 PHP 等价的吗?否则我似乎会遇到可扩展性问题。
I have a client/server app in which the client sends objects in the form of JSON to the server which runs a PHP script and then places this data into a database.
The problem is that decoding is done with the json_decode function which seems to work on strings not streams. Is there a way to take the inpustream from the HTTP request and use a streaming JSON parser to reduce the memory foot print.
I come from a java background where there are a couple of frameworks like jackson , xtream for this. Is there a PHP equivalent? Otherwise it seems I'll run into scalability issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我编写了一个可以运行的纯 PHP JSON 流解析器在溪流上。希望这是您正在寻找的东西。
I wrote a little pure PHP JSON streaming parser that works on streams. Hope that it's the kind of thing you're looking for.
您可能想尝试自己解析 php://input 流(使用 fopen()),但恕我直言,每个 SAPI(apache-mod-php、fastcgi)都在将其发送到 php 之前等待 HTTP 请求的结束,因此它不会'没有用处。
You may want to try parse yourself the php://input stream (with fopen()) but IMHO every SAPI (apache-mod-php, fastcgi) are waiting the end of the HTTP request before sending it to php so it won't be useful.