I am writing an android app to connect to PHP web service and through my searches in internet I faced file_get_contents('php://input'); and I understood some parts of it's functionality but I still don't get it. What are php://input or php://stdin or stuff like that?
I've read http://php.net/manual/en/function.file-get-contents.php and I confused much more.
Please explain it completely.
The information comes from here: http://www.php.net/manual/en/wrappers.php.php
When information is sent to the server via a POST request, it is saved in a temporary file.
The command file_get_contents('php://input') reads the raw information sent to PHP -- unprocessed before it ever gets put into $_POST or $_REQUEST super globals.
This technique is often used when someone is uploading a file, such as an image.
EDIT: removed $_GET
Related
I need to store XML data sent over HTTP POST to my server. In the log files I see that the data is successfully sent to my server. But I have no idea how to get the data.
I tried to catch them with the php://input stream like in the code below. The problem I see is that php://input is just read when the file containing the code is called.
$xml = file_get_contents("php://input");
$var_str = var_export($xml, true);
file_put_contents('api-test/test.txt', $var_str);
Is there any way to set some kind of listener/watcher to the php://input stream? Maybe PHP is the wrong technology to realize this. Is there some other way like AJAX?
The problem I see is that php://input is just read when the file containing the code is called.
Yes.
That's how PHP (in a server-side programming context) works.
The client makes an HTTP request to a URL
The server receives the HTTP request and determines that that URL is handled by a particular PHP program (typically by matching the path component of the URL to a directory and file name unless the Front Controller Pattern is being used)
The PHP program is executed and has access to data from the request
The server sends the output of the PHP program back
Is there any way to set some kind of listener/watcher to the php://input stream?
You get a new stream every time a request is made. So the typical way to watch it is to put a PHP script at the URL that the request is being made to.
Then make sure each request is made to the same URL.
(If you need to support requests being made to different URLs, then look into the Front Controller Pattern).
Maybe PHP is the wrong technology to realize this.
It's a perfectly acceptable technology for handling HTTP requests.
Is there some other way like AJAX?
Ajax is a buzzword meaning "Make an HTTP request with JavaScript". Since you are receiving the requests and not making them, Ajax isn't helpful.
I'm strugling with this for a couple of days now. I read articles and other stuff and I can't find a solution. So.
I'm working on a ski resort website based on Wordpress which has to read data from ski conditions website. The owners of the API are sending me XML data via URL: http://snezni-telefon.si/svrh_xml_test.php and they are telling me to read it with POST request. I tried everything from file_get_contents to simplexml requests and nothing works. So my question is:
How to read XML data generated in PHP file from another server with $_POST request?
Thank you!
EDIT: I tried simplexml_load_file, wp_remote_post and file_get_contents.
EDIT #2: So after going back and forth with my client they told me everything that they are doing with the script. So they have a script which contains and XML in string form, which is calling my script in which I have to get $_POST['XML'] variable. So if I understand correctly I just have to parse that $_POST variable which is simple and logical but I can't seem to make it work. Any suggestions?
I am working with another company to build a customer billing interface to their reporting software. Their system is built using ASP.NET/IIS and my system is built using PHP/Apache. They have an API that sends/receives JSON data over POST using a content-type of application/json; charset=UTF-8
I wrote a wrapper for sending data to them which they receive just fine, and also set up an endpoint for them to send data to which works just fine when I test it with cURL or Chrome's "Postman" extension, but I can't see any data they send me.
Basically my endpoint grabs the raw post data from php://input like this:
$pData = file_get_contents('php://input');
$pData = json_decode($pData);
As stated before this works great when I use postman, but when they send the request I see no raw data, nothing in $_POST and of course nothing in $_GET. I have even tried $HTTP_RAW_POST_DATA which gives me the same result as php://input.
I am logging all requests and can see they are making the request to my endpoint, but there is no content-length given so I can't tell if the data is even making it to me. Is there something I am missing on my end that would cause me to miss the raw data from an ASP.NET application? Or is it safe to assume the issue is on their end?
Thanks
Issue was on their end apparently. index.php was under a couple directories like https://www.domain.com/dir1/dir2/index.php - They were posting to https://www.domain.com/dir1/dir2 which apparently didn't work but posting to https://www.domain.com/dir1/dir2/ worked fine. Not sure why that was an issue for ASP.NET/IIS and not for PHP/Apache, cURL from a linux terminal, Postman, and http://hurl.it
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If i read file upload script I see almost everywhere the following line of code to read the file contents
file_get_contents('php://input');
But unfortunately can't really understand it. Can someone please help what does it actually mean?
Although your post isn't too detailed in what you have tried to accomplish and most importantly, how, I will attempt to provide some guidelines to solving your problem.
1. How do uploads work?
The first thing you must understand, how uploads work with PHP. The file you upload is sent as the body part of the HTTP request.
This body part however may be encoded. Once such encoding may be multipart/form-data, where the body is divided into separate parts. PHP however does some special processing to this, see later on in this answer.
An other method is to send the file itself completely unencoded as the body part of a PUT request, sent by for example XMLHttpRequest. This would enable your code to actually work, but again, let's discuss this later.
2. How does PHP handle uploads?
PHP has some special magic if you use multipart/form-data. If you use this, the contents of php://input will not be available, instead you must use the $_FILES array to handle your upload.
If you on the other hand want to process the upload yourself or you used the PUT method and your upload doesn't require special processing, you can just read all contents of php://input and use them any way you like.
For more details on uploads please see the PHP manual.
3. How to submit files from the client?
There are several methods. The most basic method is to simply create a form that submits the file as a HTTP POST upload and then handle it as described above.
If you want a progress bar, you can use some Flash like the YUI uploader. This little file will still send a HTTP POST, but you will have access to how much was uploaded via JavaScript.
Finally, if your browser supports the HTML5 File API, you can get the contents of a local file and upload it via HTTP POST or HTTP PUT according to your needs. For more details see this tutorial.
4. Debugging problems
Stuff tends to break, so you'll need to be able to debug it. Your first weapon of choice would be some sort of network dump from your browser. If you have Chrome, the tools are build in, for other browsers you might have to download some extensions.
You need to look at the request and see if the data is there and it's encoded correctly. As a web developer you should posses a deep understanding of HTTP, so if you don't read up on it. There is no excuse for not knowing HTTP.
If you are sure your data arrives on the server side, you should learn to debug PHP. First of all, take a look at your superglobals. Is everything there you sent? Or is something missing?
If you don't have the file you sent, you may have hit the configured filesize limits for uploads, either in your webserver or in your PHP. As to which the culprit is, you will have to find out for yourself.
Also, be sure to set error_reporting to E_ALL & E_NOTICE so you don't miss problems with your code.
Finally, if nothing else helps, you will need to learn to debug with xdebug. Again, there is no excuse whatsoever for a web developer for not being able to debug your own code. None.
See http://www.php.net/manual/en/wrappers.php.php.
php://input is a "special file" which contains the input that was sent to the PHP script. On a web server that means the contents of the HTTP request body. file_get_contents simply reads the entire contents, like from a regular file.
In short: that line gets the content of the HTTP request body.
However: php://input is not populated when using multipart/form-data
I'm writing a very simple file sharing site in JS and PHP. I've got a drag/drop working, so the browser gets a file object upon drop, and from there I tried to send in a xhr request to an upload page. However, I can't seem to just drop a binary file object in a request header, and so was wondering how I'd go about base64 encoding it.
In PHP I'd use base64_encode, but I'm not even at the PHP page yet. Maybe you could suggest an alternative method to my current one?
Oh, and in the PHP that receives it, it writes to a semi-random file after base64_decodeing the file.
EDIT: I worked around it, but there isn't really a good answer. Thanks for helping!
Here's my demo: http://bernsteinbear.com/up
There is a function in the works that is currently only supported in Firefox, xhr.sendAsBinary, but for now you can do the Base64 encoding in Javascript with this custom function:
http://www.webtoolkit.info/javascript-base64.html
Alternatively, you can implement sendAsBinary yourself, as seen here:
http://hublog.hubmed.org/archives/001941.html
Just be aware that the Base64 method is currently the most compatible method.
Is there any reason you aren't using something like the Valumns File Uploder? I don't know why you're wanting to add a binary file "as a request header" (that makes little sense to me), but having to base64 encode it before sending seems a bit silly when HTTP can handle sending binary data in both directions quite easily (example with forms). Then again, I'm unfamiliar with the File API, so I'm not sure what special things you might be doing with it (are you transforming the file at all before sending?). Maybe I'm missing the point of this exercise.