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个回答



你所要求的内容不是很清楚。</ p>


“这是一个 GET请求方法对吗?那么作者如何欺骗
浏览器将其作为POST请求报告而不使用HTML
表单?“</ p>
</ blockquote>

是的,你是对的,作者没有提到他如何发布数据,比如使用HTML表单。</ p>

作者可能在他的浏览器中使用了一个rest客户端插件。 也许是类似的插件。</ p>

< blockquote>

我可以用CURL实现类似的东西</ p>
</ blockquote>

当然可以。</ p>

其实你的问题是 不在上下文中。或者</ p>

因此,您可能需要使用更正后的标签更详细地阐明问题。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问