访问原始PHP POST数据中的变量 [英] Accessing Variables in Raw PHP POST Data

查看:63
本文介绍了访问原始PHP POST数据中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在php中解析Json

Possible Duplicate:
Parse Json in php

我正在使用一个API,当用户执行操作时,该API将POST请求发送到我的PHP脚本.我可以轻松地将原始POST数据记录到文件中,例如:

I am working with an API that sends a POST request to my PHP script when a user performs an action. I can easily log the raw POST data to a file, for example:

$rawPostData = file_get_contents('php://input');
$all = date("F j, Y, g:i a") . " " . $rawPostData . "\r\n";
file_put_contents("Activity.log", $all, FILE_APPEND);

将其写入日志文件:

October 1, 2012, 12:34 pm [{"changed_aspect": "media", "subscription_id": 2421759, "object": "user", "object_id": "931456", "time": 1349120084}]

我唯一感兴趣的是object_id值.如何在原始POST数据中访问此值?我已经尝试了很多,并在许多论坛中搜索了解决方案.看起来很简单,但我似乎无法理解.有什么想法吗?

The only thing I am interested in is the object_id value. How can I access this value in the raw POST data? I've tried a ton and searched a number of forums for a solution. It seems simple enough but I can't seem to get it. Any ideas?

推荐答案

您的帖子数据为JSON,因此请使用 json_decode 将其转换为包含对象的数组并访问object_id属性

Your post data is JSON so use json_decode to turn it into an array containing anobject and access the object_id property

$rawPostData = file_get_contents('php://input');
$json = json_decode($rawPostData);
$json = $json[0];
$all = date("F j, Y, g:i a") . " " . $json->object_id. "\r\n";
file_put_contents("Activity.log", $all, FILE_APPEND);

这篇关于访问原始PHP POST数据中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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