使用PHP将JSON POST记录到文件 [英] Recording a JSON POST to File Using PHP

查看:94
本文介绍了使用PHP将JSON POST记录到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将发布到服务器的数据记录到文本文件中.正在发送到我的服务器的数据的示例位于以下位置:

I'm trying to record data that is being posted to my server to a text file. An example of the data that is being sent to my server is located here:

http://dev.datasift.com/docs/推送/样本输出文件基于连接器

在该页面上显示:

对于所有基于文件的连接器,有效载荷是一个包含元数据的JSON对象以及一个包含流中JSON对象的数组."

"For all file-based connectors, the payload is a JSON object containing metadata plus an array containing the JSON objects from your stream."

在将数据发送到的位置进行数据筛选的位置,我有以下PHP:

I have the following PHP at the location I have datasift sending data to:

<?php
$myFile = "testFile.txt";
$phpObj = json_decode($_POST['json']);
file_put_contents($myFile,$phpObj);
echo '{ "success": true }';
?>

我知道正在发送数据,但是文本文件中没有任何记录.每次都是空白.不幸的是,我不知道该去哪里.有什么想法吗?

I know data is being sent, but nothing is being recorded in my text file. It's just blank every time. I have no idea where to go from here unfortunately. Any ideas?

推荐答案

我认为您想获取POST的原始内容,这对POST和PUT都适用:

I think you want to get the raw content of the POST, This works for me with both POST and PUT:

$phpObj = json_decode(file_get_contents("php://input"));

$_POST包含来自x-www-form-urlencoded内容的数组,而不是json.

$_POST contains the array from x-www-form-urlencoded content, not json.

希望能为您指明正确的方向:)

Hope that points you in the right direction :)

@ user4035是正确的...您还试图将php对象保存到文件中...这就是我会做的事情:

@user4035 is right... your also trying to save a php object to a file... This is what i would do:

<?php
$jsonString = file_get_contents("php://input");
$myFile = "testFile.txt";
file_put_contents($myFile,$jsonString);
echo '{ "success": true }';
?>

这篇关于使用PHP将JSON POST记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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