slim php (explanation of “request body” documentat

2019-08-30 11:24发布

问题:

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.