laravel 在 php 脚本中访问已发布的 Json 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16868120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing posted Json data in php script
提问by Yev
I'm using the Requests for PHPlibrary to POST some json data to another PHP script (I'm using Laravel's Response::json method to generate the json output:
我正在使用PHP库的请求将一些 json 数据发布到另一个 PHP 脚本(我使用 Laravel 的 Response::json 方法来生成 json 输出:
public function postIndex()
{
$input = Input::get();
$data = Response::json($input);
$url = 'http://mydomain.com/emails/events';
$response = Requests::post($url, array('Content-Type' => 'application/json'), $data);
return $response->status_code;
}
I need the script on the receiving end (http://mydomain.com/emails/events) to decode and process the json, but I'm having a hard time accessing it. I setup a simple test script that emails me the contents of $_POST, but it comes up empty every time.
我需要接收端 ( http://mydomain.com/emails/events)的脚本来解码和处理 json,但我很难访问它。我设置了一个简单的测试脚本,将 $_POST 的内容通过电子邮件发送给我,但每次都显示为空。
$post_data = print_r($_POST,true);
mail("[email protected]","post data",$post_data);
What am I doing wrong here?
我在这里做错了什么?
回答by sectus
PHP do not parse Json POST. So you need to get raw post data like this:
PHP 不解析 Json POST。所以你需要像这样获取原始帖子数据:
$data = file_get_contents("php://input");
Info about php php wrappers
关于 php php 包装器的信息