Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get request content (body) in PHP?

Tags:

php

request

I am sending a request to a php server with a XML in the content:

POST /index3.php HTTP/1.0
Connection: Close
Accept: application/xml
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?>
<root />

I've checked in globals vars (like $_GET, $_POST, $_ENV, $_FILES, $_REQUEST...) but they are all empty.

How could I retrieve the content in the server?

like image 540
Salazar Avatar asked Aug 25 '11 08:08

Salazar


People also ask

How do I get request body from post request?

To retrieve the body of the POST request sent to the handler, we'll use the @RequestBody annotation, and assign its value to a String. This takes the body of the request and neatly packs it into our fullName String. We've then returned this name back, with a greeting message.

Can we send body GET request?

So, yes, you can send a body with GET, and no, it is never useful to do so. This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress). Yes, you can send a request body with GET but it should not have any meaning.

What is $_ request in PHP?

PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag.


2 Answers

Try this

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

From the manual:

php://input is a read-only stream that allows you to read raw data from the request body.

like image 189
Znarkus Avatar answered Oct 24 '22 13:10

Znarkus


Use file_get_contents("php://input") (manual).

In PHP older than 7.0 you could also use $HTTP_RAW_POST_DATA (depending on always_populate_raw_post_data setting).

like image 39
piotrp Avatar answered Oct 24 '22 14:10

piotrp