How to post JSON to PHP with curl

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

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

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}}
link | edit | flag

78% accept rate
Could you please copy-paste your .php file as well? Are you sure that the URL localhost:3570/index.php/trainingServer/screencast.json runs your script? It doesn't look like a PHP URL. –  pts May 1 '09 at 22:11
@pts; Peter is using an MVC framework of some kind, take a look at index.php in that URL. –  Alan Storm May 1 '09 at 23:59
@pts I'm using Delphi for PHP (hence the :3570) and the Recess MVC framework w/out .htaccess file, hence the index.php/ in the url. –  Peter Turner May 4 '09 at 13:22
1  
Don’t forget to send it as application/json . –  Gumbo Aug 16 '09 at 11:30
You need to escape your quotation marks, dude. –  Peter Turner Jan 20 at 11:50
show 1 more comment

4 Answers

up vote 20 down vote accepted

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

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

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

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.

link | edit | flag
Nice answer. +1 –  Jordan S. Jones May 1 '09 at 22:32
1  
Doing json_decode(file_get_contents("php://input"), true) worked. Thanks –  Peter Turner May 4 '09 at 14:25
Really helped my situation as well! Thanks Emil H! –  Steve Oct 13 '09 at 17:05

Normally -d is interpreted as form-encoded. You need the -H parameter:

curl -v -H "Content-Type: application/json" -X POST -d '{"screencast":{"subject":"tools"}}' / http://localhost:3570/index.php/trainingServer/screencast.json

link | edit | flag

You should escape the quotes like this:

curl 
-
i 
-
X POST 
-
d 
'{/"screencast/":{/"subject/":/"tools/"}}'
  
/

  http
: //localhost:3570/index.php/trainingServer/screencast.json
link | edit | flag

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

Try changing your curl request to:

curl 
-
i 
-
X POST 
-
d 
'json={"screencast":{"subject":"tools"}}'
  
/

      http
: //localhost:3570/index.php/trainingServer/screencast.json

and see if that helps any.

link | edit | flag
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值