How to post JSON to PHP with curl
来源:互联网 发布:面板数据的主成分分析 编辑:程序博客网 时间:2024/05/16 14:11
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}}
4 Answers
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.
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
You should escape the quotes like this:
curl -i -X POST -d '{/"screencast/":{/"subject/":/"tools/"}}' /
http://localhost:3570/index.php/trainingServer/screencast.json
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.
- How to post JSON to PHP with curl
- How to switch from POST to GET in PHP CURL
- How to post quotes in cURL
- How to Make cURL works with cmake
- curl with post json
- How to enable cURL in PHP / XAMPP
- how to install curl
- Using curl to upload POST data with files
- How to implement COMET with PHP
- 转:How to implement COMET with PHP
- How to code APNS with PHP?
- How to implement COMET with PHP
- pycurl post json data to php
- [iOS/Android开发之WebService]How to write a webservices using php with json format
- How to use json with as3corelib in flex( JSON )
- How to post SO's or PO's with X++
- how to post URL with Image on facebook by ShareKit
- How to POST a django form with AJAX & jQuery
- 查询某数据库中的表 Mysql
- PHP中的final/static
- 清除数据库日志方法
- JavaScript内置对象属性及方法
- MFC消息映射与处理
- How to post JSON to PHP with curl
- proftpd的简单配置
- ARP攻击原理及解决方法
- ORACLE PROFILE文件管理
- 1
- fck与smarty的结合使用
- 数据库获取前n条记录/分页
- (转)wcsncpy详解
- Linux libcurl使用(一)
application/json
. – Gumbo♦ Aug 16 '09 at 11:30