I am having difficulties figuring this out. I can see I am posting XML and I see the length of it but I cannot figure out how to display or access the POST DATA
CONTENT-TYPE: text/xml<br />
CONTENT-LENGTH: 640<br />
separately I have
curl -k -H 'content-type: text/xml' -d 'XML_DATA_HERE' https://ip/page.php
Try
$xml = file_get_contents('php://input');
http://docs.php.net/manual/en/wrappers.php.php says:
php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype="multipart/form-data".
Related
POST
On my local machine, I want to use cURL as an HTTP client like so:
curl -X POST -F 'email=derp#example.com' -F 'password=blink182' http://example.com
The above curl statement uses the HTTP POST method which can be retrieved in PHP like so:
echo $_POST['email']; // derp#example.com
echo $_POST['password']; // blink182
php://input
However, what I really wanted is the data from the PHP input stream php:://input and not from the POST method $_POST.
The PHP input stream can be retrieved in PHP like so:
$input = json_decode( file_get_contents( 'php://input' ) );
echo $input->email; // derp#example.com
echo $input->password; // blink182
Which brings me to my question, how to send PHP input stream data using curl?
From the PHP website:
php://input is a read-only stream that allows you to read raw data from the request body. php://input is not available with enctype="multipart/form-data".
So if you specify the Content-Type as application/json using -H "Content-Type: application/json" you ought to get it in the input stream
complete example:
curl -X POST https://reqbin.com/echo/post/json
-H 'Content-Type: application/json'
-d '{"email":"derp#example.com","password":"blink182"}'
I am getting post data like this
$data = file_get_contents('php://input');
I want the data put back to the stream, so I modified the method following instaed of above method
$stream = fopen('php://temp', 'w+');
stream_copy_to_stream(fopen('php://input', 'r'), $stream);
rewind($stream);
But it is not working. Can any one tell where I did wrong?
This case is need in wordpress plugin developement. If one plugin reads the post data, other plugin not able to get that data.
Thanks
As stated in the documentation, php://input can only be read once and doesn't support seek operations.
The only way to 'read' the input stream multiple times is through passing its copy in your program.
PHP input stream contains request body and your sample will work only if request will contain body.
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d 'sample body' "YOUR_ADDRESS"
For check add to code:
echo fgets($stream);
php://input is not available with enctype="multipart/form-data".
http://php.net/manual/en/wrappers.php.php
I'm sending a POST request using WebClient.UploadData() method (C#) to my webserver. The packet sent to my webserver looks like so:
POST / HTTP/1.1
Host: {ip}
Content-Length: {length}
Expect: 100-continue
Connection: Keep-Alive
{buffer_content}
As the {buffer_content} is nowhere assigned in the $_POST array, I have the following question...
Question: How do I read the {buffer_content} with PHP?
I've stumbled upon file_get_contents('php://input'), but I'm unsure whether that is recommended to do.
Use the php://input stream:
$requestBody = file_get_contents('php://input');
This is the recommended way to do this and, in PHP 7.0, the only way. Previously, there was sometimes a global variable called $HTTP_RAW_POST_DATA, but whether it existed would depend on an INI setting, and creating it hurt performance. That variable was deprecated and removed.
Beware that prior to PHP 5.6, you can only read php://input once, so make sure you store it.
Once you have your body, you can then decode it from JSON or whatever, if you need that:
$requestBody = json_decode($requestBody) or die("Could not decode JSON");
I am running Apache2 and PHP 5 on Linux, and I'm getting some strange behavior with the php://input stream.
For some GET requests the stream is not empty like it should be. Instead, the php://input stream contains the entire GET request. I have worked around the issue but I would like to know if I should file a bug about this, or if it is "desired but undocumented" behavior.
Details
Early in the request processing, I call:
$in = file_get_contents('php://input');
if ( !empty($in) )
$post_data = json_decode($in);
if ( !empty($in) && is_null($post_data) ) {
// output some error info and exit
}
Usually when a request does not have a body then $in is empty and all is right with the world. But sometimes a GET request will have a body, and that body will be the entire request. Of course you can't json-decode that data, and the error condition gets hit.
This only happens with some requests. For example, this request does not exhibit the error:
GET /os/invitations/kkkkkk HTTP/1.1
Host: our.machine.com
Content-Type: application/json
Authorization: Basic aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa==
But this request, which is routed through some proxies and VPNs, does trigger the error.
GET http://some.proxy.at.some.big.company.com:7080/cvp-out/cmmproxy/os/invitations/d66065566dba541c8ba6a70329684645 HTTP/1.1
Content-Type: application/json
Authorization: Basic aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa==
Clientid: abc
User-Agent: Java/1.6.0
Host: some.proxy.at.some.big.company.com:7080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
X-Remote-Addr: 53.231.244.171
X-Remote-Host: 53.231.244.171
X-Server-Name: some.proxy.at.some.big.company.com
X-Server-Port: 7080
X-Scheme: http
I spent hours treating this like a routing/dispatch problem, but it turned out to be our code. The fix was, of course, to only read from the input stream when you are expecting data:
if ( in_array( $_SERVER['REQUEST_METHOD'], array('PUT', 'POST') )) {
$in = file_get_contents('php://input');
if ( !empty($in) )
$post_data = json_decode($in);
}
Is this a known issue? Does it happen unpredictably? Should I file a bug?
As far as i know, that's not an error. We understand that a GET request shouldnt have a body, but in the docs of php:// they say nothing about wich types of requests will generate an input, so it could be any method. And for sure it is not limited to POST, since the mention at least PUT and PROPFIND.
So at any rate, your solution is a must.
I need to write a PHP page which would accept an XML document sent over a POST request like this:
POST /mypage.php HTTP/1.1
Host: myhost.com
Content-Type: application/xml
Content-Length: ...
<?xml version="1.0" encoding="utf-8"?>
<data>
...
</data>
This is not data from some HTML form, just a plain XML document.
How can I access this XML in my PHP code?
Read from php://input. For example, you could use:
$rawdata = file_get_contents('php://input');
or
$rootNode = simplexml_load_file('php://input');
The alternative, using $HTTP_RAW_POST_DATA, works, too - but it's slower and needs the PHP configuration always_populate_raw_post_data.
Try the $HTTP_RAW_POST_DATA variable or the php://input stream.
http://us.php.net/manual/en/reserved.variables.httprawpostdata.php
$HTTP_RAW_POST_DATA should be available assuming the content-type of the request was not multipart/form-data
$HTTP_RAW_POST_DATA
or
php://input
You probably want to use the PHP input. Something like
$postText = trim(file_get_contents('php://input'));
Any of these would work:
$HTTP_RAW_POST_DATA variable or the php://input stream.
However, for the
$HTTP_RAW_POST_DATA variable
to work, you might need to change .ini setting in your configuration file
If you use print_r($_POST); you will be able to see what you got. Unless i'm missing something...
Edit: nvm, totaly forgot about Raw Data :/