如何使用curl将JSON发布到PHP [英] How to post JSON to PHP with curl

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

问题描述

我可能还差得远,但是我一直在整个下午尝试运行在此隐式PHP框架教程中,请使用curl post命令.我不明白的是PHP应该如何解释我的POST,它总是作为一个空数组出现.

I may be way off base, but I've been trying all afternoon to run the curl post command in 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.

curl -i -X POST -d '{"screencast":{"subject":"tools"}}'  \
      http://localhost:3570/index.php/trainingServer/screencast.json

(其中的斜线只是让我看起来不像个白痴,而是我使用PHP 5.2在Windows上执行此操作,也曾在Linux服务器上尝试过,该版本与Linux curl相同)

(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)

肯定有一些我想念的东西,因为它看起来很简单,帖子的解释不正确,如果可以的话,一切都会很好.

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.

这就是我得到的:


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}}

推荐答案

Jordans关于为什么未填充$ _POST-array的分析是正确的.但是,您可以使用

Jordans analysis of why the $_POST-array isn't populated is correct. However, you can use

$data = file_get_contents("php://input");

仅检索http正文并自己处理.参见 PHP输入/输出流.

to just retrieve the http body and handle it yourself. See PHP input/output streams.

从协议的角度来看,这实际上是更正确的,因为您实际上并没有真正处理http多部分表单数据.另外,在发布请求时,将application/json用作内容类型.

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.

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

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