PHP Web Fundamentals Reading the POST Request Body - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Web Fundamentals Reading the POST Request Body - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Tuesday, May 28, 2019

PHP Web Fundamentals Reading the POST Request Body

PHP Web Fundamentals




Reading the POST Request Body

Problem

You want direct access to the body of a request, not just the parsed data that PHP puts in $_POST for you. For example, you want to handle an XML document that’s been posted as part of a web services request.

Solution

Read from the php://input stream:

         $body = file_get_contents('php://input');

Discussion

The superglobal array $_POST is designed for accessing submitted HTML form variables, but it doesn’t cut it when you need raw, uncut access to the whole request body. That’s where the php://input stream comes in. Read the entire thing with file_get_contents(), or if you’re expecting a large request body, read it in chunks with fread().

If the configuration directive always_populate_raw_post_data is on, then raw post data is also put into the global variable $HTTP_RAW_POST_DATA. But to write maximally portable code, you should use php://input instead—that works even when always_populate_raw_post_data is turned off.



No comments:

Post a Comment

Post Top Ad