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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题