Re: php://input
| From: | Frank Arensmeier | Date: | Sun, 15 Jan 2012 09:22:16 +0000 |
| Subject: | Re: php://input | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316298@lists.php.net to get a copy of this message | ||
15 jan 2012 kl. 06.18 skrev Adam Tong:
> Hi,
>
> I am trying to read variables from input method.
> I am using this tuorial:
>
> http://www.lornajane.net/posts/2008/Accessing-Incoming-PUT-Data-from-PHP.
> Here is my code:
> <?php
> if($_SERVER['REQUEST_METHOD'] == 'GET') {
> echo "this is a get request\n";
> echo $_GET['fruit']." is the fruit\n";
> echo "I want ".$_GET['quantity']." of them\n\n";
> } elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
> echo "this is a put request\n";
> parse_str(file_get_contents("php://input"),$post_vars);
> echo $post_vars['fruit']." is the fruit\n";
> echo "I want ".$post_vars['quantity']." of them\n\n";
> }
> ?>
>
> I am using the firefox extension "poster" to run this example. GET
> works fine but when using PUT, file_get_contents("php://input")
> returns an empty string.
>
> I found a bug related to this: 9z*ñê5êF £
> ƒ;¯�https://bugs.php.net/bug.php?id=51592
>
> I am using xampp on win7 (
> + Apache 2.2.17
> + MySQL 5.5.8 (Community Server)
> + PHP 5.3.5 (VC6 X86 32bit) + PEAR)
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
Hi Adam.
Although I've never worked with PUT/DELETE requests, here are my thoughts.
1) According to the manual, file_get_contents only allows URL's as filenames if "fopen
wrappers" are enabled. Make sure that this is the case (have a look at the settings in your php
ini file). Do you get any data when changing 'file_get_contents' to e.g. (as found here:
http://php.net/manual/en/features.file-upload.put-method.php)?
<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
/* Close the streams */
fclose($fp);
fclose($putdata);
?>
2) Have a look in your Appache log files and make sure the client is actually making a valid PUT
request.
/frank