我正在使用的工具:Slim Framework 和 RedBeanPHP
我正在研究这篇文章:http ://www.ibm.com/developerworks/library/x-slim-rest/ ,尤其是这种方法:
// handle POST requests to /articles
$app->post('/articles', function () use ($app)
{
try
{ // get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
// store article record
$article = R::dispense('articles');
$article->title = (string)$input->title;
$article->url = (string)$input->url;
$article->date = (string)$input->date;
$id = R::store($article);
// return JSON-encoded response body
$app->response()->header('Content-Type', 'application/json');
echo json_encode(R::exportAll($article));
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
我运行应用程序或刷新/输入您喜欢的任何内容,但在我的控制台中没有看到文章中的任何 POST 报告?
我期待看到:http ://www.ibm.com/developerworks/library/x-slim-rest/#f5
在我的控制台中。
相反,我得到:http ://acookson.org/?attachment_id=1221
这是一个GET请求方法吧?那么作者是如何在不使用 HTML 表单的情况下欺骗浏览器将其报告为 POST 请求的呢?
有了这条线,我听到你说:
// get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
好吧,我正在复制这篇文章,就像不喜欢一样 - 也没有任何东西添加到数据库中。
我可以用 CURL 实现类似的东西
$ curl -i -X POST -H 'Content-Type: application/json' -d '{"id": "3", "title": "Programming with C++", "url":"http:\/\/www.google.com\/programming\/C++","date":"2013-01-10"}' http://example.localhost/articles
HTTP/1.1 200 OK
Date: Mon, 01 Apr 2013 08:52:50 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.6-1ubuntu1.2
Content-Length: 116
Content-Type: application/json
POST
[{"id":3,"title":"Programming with C++","url":"http:\/\/www.google.com\/programming\/C++","date":"2013-01-10"}]
将第 3 行添加到我的数据库中:
mysql> select * from articles\G;
*************************** 1. row ***************************
id: 1
title: Search and integrate Google+ activity streams with PHP applications
url: http://www.ibm.com/developerworks/xml/library/x-googleplusphp/index.html
date: 2012-07-10
*************************** 2. row ***************************
id: 2
title: Getting Started with Zend Server CE
url: http://devzone.zend.com/1389/getting-started-with-zend-server-ce/
date: 2009-03-02
*************************** 3. row ***************************
id: 3
title: Programming with C++
url: http://www.google.com/programming/C++
date: 2013-01-10
3 rows in set (0.00 sec)
我错过了什么?