Reputation: 8702
I'd like to perform an operation on the php://input
stream but also return it.
For example, what I'd like to achieve is that:
php://input ==> OPERATION ==> php://input
Is that possible to make something like that ?
$input = fopen("php://input", "r");
$output = fopen("php://input", "w");
while (($buffer.= fgets($input, 1024)) !== false) {
// Do something with that buffer
// ??
fwrite($output, $buffer);
}
fclose($output);
fclose($input);
Upvotes: 2
Views: 543
Reputation: 158250
If you are fine with one of the operations supported as a filter in php, you can use the php://filter
fopen wrapper.
Let's say you want to base64 decode the data for example:
$data = file_get_contents('php://filter/read=convert.base64-decode/resource=php://input');
Or:
$input = fopen('php://filter/read=convert.base64-decode/resource=php://input');
// now you can pass $input to somewhere and every read operation will
// return base64 decoded data ...
However, the set of operations supported as a filter in PHP is quite limited. If it does not fit your needs I would suggest to wrap the file pointer, in a class maybe. Here comes a very basic example, you might add buffering, caching or whatever...
class Input {
public static function read() {
return $this->process(file_get_contents('php://stdin'));
}
public function process($data) {
return do_whatever_with($data);
}
}
Then in the application code use:
$input = Input::read();
Upvotes: 2