dongzhao1865 2013-04-01 10:18
浏览 49

POST请求方法通过调用GET,SlimPHP,RedBeanPHP

Tools I'm using: Slim Framework & RedBeanPHP

I'm studying this article: http://www.ibm.com/developerworks/library/x-slim-rest/ and especially this method:

// 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());
}
});

I run the application or refresh/enter whatever you like, but don't see any reporting of POST in my console as in the article?

I'm expecting to see: http://www.ibm.com/developerworks/library/x-slim-rest/#f5

in my console.

Instead I get: http://acookson.org/?attachment_id=1221

It's a GET request method right? So how does the author trick the browser into reporting it as a POST request without the use of a HTML form?

With this line I hear you say:

// get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body); 

Well I'm copying the article like for like and nope - nothing added to the database either.

I can acheive something similar with 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"}]

with row 3 added to my database:

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)

what am I missing?

  • 写回答

1条回答 默认 最新

  • dongsheng1238 2013-12-27 09:23
    关注

    It's not very clear what you have asked.

    "It's a GET request method right? So how does the author trick the browser into reporting it as a POST request without the use of a HTML form?"

    Yes, you are right, the author haven't mentioned anything on how he is posting the data, like using a HTML form.

    Author might have used a rest client plugin in his browser. Perhaps a similar addon.

    I can achieve something similar with CURL

    Sure you can.

    Actually your problem is not within the context slim or php.

    So you might need to clarify the problem in more detail with corrected tags.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口