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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序