I'm writing a script that is registered as an endpoint for a webhook. I know that it's successfully registered because I'm writing the header of every request to my server logs. Here's a sample:

Content-Type: text/xml; charset=UTF-8
User-Agent: Jakarta Commons-HttpClient/3.1
Host: =={obfuscated}== 
Content-Length: 1918

我注册的API正在将一个JSON对象发布到我的脚本中,我想用PHP解析这个对象.从请求头中可以看到,有一个漂亮的大而胖的JSON对象正在等待解析.这似乎很简单,但事实并非如此.

起初,我try 使用$_POST['json']或只使用$_POST,但由于数据不在数组中,我真的不确定如何以这种方式访问它.

我试过用file_get_contents('php://input')fopen('php://input', 'r'),加json_decode()和不加json_decode()都不行.我不能使用http_get_request_body(),因为我使用的服务器没有PECL,这不在我的控制范围之内.

Are there any other ways to interact with the POST-ed JSON object that I'm missing? Thanks!

推荐答案

Thanks to others for the input. It turns out that I just needed

$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE); //convert JSON into array

其中,json_decode中的第二个参数以数组的形式返回对象.

Hope this helps someone else!

Json相关问答推荐

如何将自定义卡保存到dart/flutter中的共享偏好设置?

从Json响应中为需要每个值的Post请求提取多个值

使用WSO2 JsonTransform Mediator对空值执行JsonExceptionUndeletedOperationException

在Ansible中从json中提取特定数据

使用快速json库编写json可以消除所有缩进

Rust实现:高效解析任意大小的JSON数组

将具有多个级别的 json 读入 DataFrame [python]

坚持弄清楚如何使用 api 响应来调用以从不同的链接检索响应

我无法在 Go - Gin 中解析日期/时间

反序列化大型 json 对象的 JsonMaxLength 异常

从 HttpResponse 获取 json

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

Android JSON 库的性能和可用性比较

如何在java中比较来自JsonObject的空值

PostgreSQL 中的 JSON 模式验证?

使用 Retrofit 解析动态密钥 Json 字符串

JSON Schema:验证数字或空值

Sequelize - 如何仅返回数据库结果的 JSON 对象?

YAML 将 5e-6 加载为字符串而不是数字

ASP.NET Web API JSON 输出中没有时间的日期