如何使用 curl 将 JSON 发布到 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/813487/
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
How to post JSON to PHP with curl
提问by Peter Turner
I may be way off base, but I've been trying all afternoon to run the curl post commandin this recess PHP framework tutorial. What I don't understand is how is PHP supposed to interpret my POST, it always comes up as an empty array.
我可能有点偏离基础,但我整个下午都在尝试在这个 PHP 框架教程中运行curl post 命令。我不明白的是 PHP 应该如何解释我的 POST,它总是作为一个空数组出现。
curl -i -X POST -d '{"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
(The slash in there is just to make me not look like an idiot, but I executed this from windows using PHP 5.2, also tried on a Linux server, same version with Linux curl)
(那里的斜杠只是为了让我看起来不像个白痴,但我使用 PHP 5.2 从 Windows 执行了这个,也在 Linux 服务器上尝试过,与 Linux curl 相同的版本)
There must be something I'm missing because it seems pretty straightforward, the post just isn't be interpreted right, if it was, everything would work great.
一定有我遗漏的东西,因为它看起来很简单,只是没有正确解释帖子,如果是这样,一切都会很好。
This is what I get back:
这是我回来的:
HTTP/1.1 409 Conflict
Date: Fri, 01 May 2009 22:03:00 GMT
Server: Apache/2.2.8 (Win32) PHP/5.2.6
X-Powered-By: PHP/5.2.6
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
{"screencast":{"id":null,"subject":null,"body":null,
"dataUrl":null,"dataMedium":null,"createdOn":null,"author":null}}
采纳答案by Emil H
Jordans analysis of why the $_POST-array isn't populated is correct. However, you can use
Jordans 对 $_POST 数组未填充的原因的分析是正确的。但是,您可以使用
$data = file_get_contents("php://input");
to just retrieve the http body and handle it yourself. See PHP input/output streams.
只需检索http主体并自己处理即可。请参阅PHP 输入/输出流。
From a protocol perspective this is actually more correct, since you're not really processing http multipart form data anyway. Also, use application/json as content-type when posting your request.
从协议的角度来看,这实际上更正确,因为无论如何您都没有真正处理 http 多部分表单数据。另外,在发布请求时使用 application/json 作为内容类型。
回答by Jim Carrig
Normally the parameter -dis interpreted as form-encoded. You need the -Hparameter:
通常,参数-d被解释为表单编码。你需要-H参数:
curl -v -H "Content-Type: application/json" -X POST -d '{"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
回答by Jordan S. Jones
I believe you are getting an empty array because PHP is expecting the posted data to be in a Querystring format (key=value&key1=value1).
我相信您得到的是一个空数组,因为 PHP 期望发布的数据采用查询字符串格式 (key=value&key1=value1)。
Try changing your curl request to:
尝试将您的 curl 请求更改为:
curl -i -X POST -d 'json={"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
and see if that helps any.
看看这是否有帮助。
回答by Chris Knadler
You need to set a few extra flags so that curl sends the data as JSON.
您需要设置一些额外的标志,以便 curl 将数据作为 JSON 发送。
command
命令
$ curl -H "Content-Type: application/json" \
-X POST \
-d '{"JSON": "HERE"}' \
http://localhost:3000/api/url
flags
旗帜
-H: custom header, next argument is expected to be header-X: custom HTTP verb, next argument is expected to be verb-d: sends the next argument as data in an HTTP POST request
-H: 自定义标题,下一个参数应该是标题-X: 自定义 HTTP 动词,下一个参数应为动词-d: 在 HTTP POST 请求中将下一个参数作为数据发送
resources
资源
回答by Chris Knadler
You should escape the quotes like this:
你应该像这样转义引号:
curl -i -X POST -d '{\"screencast\":{\"subject\":\"tools\"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json

