slim php (explanation of “request body” documentation)

我与影子孤独终老i 提交于 2019-12-24 19:05:16

问题


I am working with Slim PHP for the first time and I am trying to understand one of the concepts. In the slim PHP documentation, it states:

Request Body

Use the request object’s getBody() method to fetch the raw HTTP request body sent by the HTTP client. This is particularly useful for Slim application’s that consume JSON or XML requests.

<?php
$request = $app->request();
$body = $request->getBody();

My question is, what is "the raw HTTP request body"? Is it just a string of all the HTML in the body of the page? What format is it stored as? What would echo $body look like? If I do var_dump($body) I get string(0)"". How do I use it?


回答1:


I'll just make it an answer rather than comment...

Raw request data is what's submitted from the browser as a body of the POST request. http://en.wikipedia.org/wiki/POST_%28HTTP%29#Use_for_submitting_web_forms

Technically it can be used to read the data from usual html forms, but this doesn't make much sense as PHP does this good enough and places everything into $_POST.

You may need to read raw data if you have some javascript that sends XML or JSON data, which is not natively accepted by PHP.




回答2:


The terms you ask for are defined in the RFC2616: Hypertext Transfer Protocol -- HTTP/1.1.

For example, in particular what a Message (Request/Response) Body is: 4.3 Message Body.

If those RFCs are new to you, grab that one an read it from top to bottom and try to understand as much as possible. You'll start to see how those things in the internet work.

Also there is version 2.0 is in the pipe with some changes:

  • Hypertext Transfer Protocol version 2.0 (Draft 04)

Just in case you're interested.



来源:https://stackoverflow.com/questions/17618716/slim-php-explanation-of-request-body-documentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!