Php post raw body

Get raw post data

To access the entity body of a POST or PUT request (or any other HTTP method):,http_get_request_body() was explicitly made for getting the body of PUT and POST requests as per the documentation http://php.net/manual/fa/function.http-get-request-body.php,This is an exemple of how to create a php api with file_get_contents(«php://input») and used in javascript using ajax with XMLHttpRequest.,This is the body of the request (a POST request). In php, what do I have to do to extract that value?

To access the entity body of a POST or PUT request (or any other HTTP method):

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

Also, the STDIN constant is an already-open stream to php://input , so you can alternatively do:

$entityBody = stream_get_contents(STDIN); 

To maintain the stream resource something like this can be helpful:

Answer by Meilani Moss

Usually data sent in a POST request is structured key/value pairs with a MIME type of application/x-www-form-urlencoded. However many applications such as web services require raw data, often in XML or JSON format, to be sent instead. This data can be read using one of two methods.,Note that neither of these methods are available when the content type is set to multipart/form-data, which is used for file uploads., Localization , Localization

php://input is a stream that provides access to the raw request body.

$rawdata = file_get_contents("php://input"); // Let's say we got JSON $decoded = json_decode($rawdata); 

$HTTP_RAW_POST_DATA is a global variable that contains the raw POST data. It is only available if the always_populate_raw_post_data directive in php.ini is enabled.

$rawdata = $HTTP_RAW_POST_DATA; // Or maybe we get XML $decoded = simplexml_load_string($rawdata); 

Answer by Zayd Zamora

This is a short guide on how to receive raw POST data in PHP. This is useful if you intend on receiving JSON or XML via a POST request.,Note that if no POST data is present, the code above will return a blank string.,The feature relies on the always_populate_raw_post_data directive being set to 1. However, the default value for this directive is 0 (off).,$HTTP_RAW_POST_DATA is a predefined PHP variable that can be configured to contain raw POST data. However, the variable was officially deprecated in PHP 5.6 and completely removed as of PHP 7.0.

In most day-to-day cases, we tend to access POST data like so:

//Retrieving a POST field in PHP $name = $_POST['name'];

Take a look at the following example:

//Get the raw POST data $postBody = file_get_contents("php://input");

Answer by Juliette McKee

I recently stumbled upon an interesting problem when trying to retrieve the raw POST body in Laravel. This happened when I was sending POST/PUT requests from AngularJS to a REST API that was built with Laravel. I did a lot of trial and error before I figured out the problem.,Now we have the raw POST data in $content, simple as that.,Lucky for us, the request instance is accessible through the Request facade in Laravel. The request instance then have the raw POST data set as its content.,The php://input is an I/O stream that you can read the raw POST data from. What is interesting though is that it can only be read once. And this is what caused me not being able to fetch it in this simple matter.

Читайте также:  Php get method name in class

Answer by Ryan Porter

These two code examples show how to send and receive data as individual fields. , These two code examples show how to send data as one block of text or «raw data», and how to receive it. , When making a request with curl we can send post data as individual fields, such as when submitting a form, or we can send the data as an one block of text. The code examples below show how to send both types of requests with PHP.

send_post_fields.php

  'value1', 'field2' => 'value2' ); $query_string = http_build_query($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); print_r($result); 

read_post_fields.php

send_post_raw_data.php

  'value1', 'field2' => 'value2' ); $body = json_encode($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); print_r($result); 

read_post_raw_data.php

Answer by Sariyah Farley

I receive data to my application via POST, however the data does not have a variable name.,The post body contains JSON data.,Why would you use file_get_content ? What do you mean with «data does not have a variable name»?,$_POST only works when you have variable names.

How can I read that data in my controller? I tried

 file_get_contents('php://input'); 

Источник

PHP: Receive raw POST data.

This is a short guide on how to receive raw POST data in PHP. This is useful if you intend on receiving JSON or XML via a POST request.

In most day-to-day cases, we tend to access POST data like so:

//Retrieving a POST field in PHP $name = $_POST['name'];

However, in certain cases, you may need to access the POST request’s body as a whole. In those cases, you will need to access the php://input stream.

The php://input stream.

PHP has a number of I/O streams that you can access. In this case, we are particularly interested in the php://input stream.

Take a look at the following example:

//Get the raw POST data $postBody = file_get_contents("php://input");

In the code above, we used the file_get_contents function to read the php://input stream into a PHP variable called $postBody.

Читайте также:  Flexbox css вертикальное выравнивание

If a POST request is sent to the script above, the entire body of the request will be assigned to the $postBody variable.

For example: When I sent three POST parameters to the script in question, the php://input stream returned the following string:

Note that if no POST data is present, the code above will return a blank string.

Don’t use $HTTP_RAW_POST_DATA.

$HTTP_RAW_POST_DATA is a predefined PHP variable that can be configured to contain raw POST data. However, the variable was officially deprecated in PHP 5.6 and completely removed as of PHP 7.0.

Besides the fact that it is no longer supported, there is also the drawback of having to mess around with .ini directives in order to get it working. This is because the feature is turned off by default on most PHP installations.

The feature relies on the always_populate_raw_post_data directive being set to 1. However, the default value for this directive is 0 (off).

Long story short: You should use the input stream approach instead.

Источник

Get raw post data

To access the entity body of a POST or PUT request (or any other HTTP method):,http_get_request_body() was explicitly made for getting the body of PUT and POST requests as per the documentation http://php.net/manual/fa/function.http-get-request-body.php,This is an exemple of how to create a php api with file_get_contents(«php://input») and used in javascript using ajax with XMLHttpRequest.,This is the body of the request (a POST request). In php, what do I have to do to extract that value?

To access the entity body of a POST or PUT request (or any other HTTP method):

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

Also, the STDIN constant is an already-open stream to php://input , so you can alternatively do:

$entityBody = stream_get_contents(STDIN); 

To maintain the stream resource something like this can be helpful:

Answer by Meilani Moss

Usually data sent in a POST request is structured key/value pairs with a MIME type of application/x-www-form-urlencoded. However many applications such as web services require raw data, often in XML or JSON format, to be sent instead. This data can be read using one of two methods.,Note that neither of these methods are available when the content type is set to multipart/form-data, which is used for file uploads., Localization , Localization

php://input is a stream that provides access to the raw request body.

$rawdata = file_get_contents("php://input"); // Let's say we got JSON $decoded = json_decode($rawdata); 

$HTTP_RAW_POST_DATA is a global variable that contains the raw POST data. It is only available if the always_populate_raw_post_data directive in php.ini is enabled.

$rawdata = $HTTP_RAW_POST_DATA; // Or maybe we get XML $decoded = simplexml_load_string($rawdata); 

Answer by Zayd Zamora

This is a short guide on how to receive raw POST data in PHP. This is useful if you intend on receiving JSON or XML via a POST request.,Note that if no POST data is present, the code above will return a blank string.,The feature relies on the always_populate_raw_post_data directive being set to 1. However, the default value for this directive is 0 (off).,$HTTP_RAW_POST_DATA is a predefined PHP variable that can be configured to contain raw POST data. However, the variable was officially deprecated in PHP 5.6 and completely removed as of PHP 7.0.

Читайте также:  Nav menu css style

In most day-to-day cases, we tend to access POST data like so:

//Retrieving a POST field in PHP $name = $_POST['name'];

Take a look at the following example:

//Get the raw POST data $postBody = file_get_contents("php://input");

Answer by Juliette McKee

I recently stumbled upon an interesting problem when trying to retrieve the raw POST body in Laravel. This happened when I was sending POST/PUT requests from AngularJS to a REST API that was built with Laravel. I did a lot of trial and error before I figured out the problem.,Now we have the raw POST data in $content, simple as that.,Lucky for us, the request instance is accessible through the Request facade in Laravel. The request instance then have the raw POST data set as its content.,The php://input is an I/O stream that you can read the raw POST data from. What is interesting though is that it can only be read once. And this is what caused me not being able to fetch it in this simple matter.

Answer by Ryan Porter

These two code examples show how to send and receive data as individual fields. , These two code examples show how to send data as one block of text or «raw data», and how to receive it. , When making a request with curl we can send post data as individual fields, such as when submitting a form, or we can send the data as an one block of text. The code examples below show how to send both types of requests with PHP.

send_post_fields.php

  'value1', 'field2' => 'value2' ); $query_string = http_build_query($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); print_r($result); 

read_post_fields.php

send_post_raw_data.php

  'value1', 'field2' => 'value2' ); $body = json_encode($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); print_r($result); 

read_post_raw_data.php

Answer by Sariyah Farley

I receive data to my application via POST, however the data does not have a variable name.,The post body contains JSON data.,Why would you use file_get_content ? What do you mean with «data does not have a variable name»?,$_POST only works when you have variable names.

How can I read that data in my controller? I tried

 file_get_contents('php://input'); 

Источник

Оцените статью