PUT access to incoming data from PHP

Dec 10, 2008 Author: Developer

Recently I started to write REST request. I started in the usual manner, php script writing, calls with GET, enabling variables using superglobalnata variable PHP - $ _GET. I wrote the code to grips POST request and use variables to find in $ _POST. Then I tried to npisha PUT request.

PHP-no built-in way to do it over and I was a little confused how to reach it. It turns out that this can be read by flow coming to PHP, php: / / input.

CODE

1
file_get_contents("php://input");

The above line I provide questionable relationship similar to what you may see a URL with a GET request. Key variables and are separated by question marks. Saved by making parsing of this monster of rejection in this one directed me that parse_str () is actually for this purpose (seriously, I wrote many PHP-so and I have no idea how I missed those things, but I always fun to "unveil") - takes questionable link and parse the variables. Look for major health warning on str_parse () --

generally you will create all variables across your local area! Parameter will undergo hard for a second and will instruct them like connecting line vzamyana - highly recommend it approaching and I use it here with my variable $ post_vars.

CODE

1
parse_str(file_get_contents("php://input"),$post_vars);

 

This load promelivata $ post_vars by connecting line of variables exactly as you expect to see from the GET request.

A simple example:

This is a small fictional example, but shows the use of REQUEST_METHOD

within the variable $ _SERVER to calculate when to "catch" the post vars. First pitch is here:

 

CODE

1
2
3
4
5
6
7
8
9
10
11
12
<?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";
}
?>

 

And this is what happens when I run the same script with two different HTTP operations. Use cURL, to show just examples, because I think this is most visible - good.

By GET:

curl "http://localhost/rest_fruit.php?quantity=2&fruit=plum"
this is a get request
plum is the fruit
I want 2 of them

By PUT:

curl-X PUT http://localhost/rest_fruit.php-d fruit = orange-d quantity = 4
this is a put request
orange is the fruit
I want 4 of them

Frequencies will you say that you should not transfer data from a PUT request will be made. But it shows how to get to coming variables and zasechesh, executed action. If you write REST service, then the correct names of resources and answers every resource that has access variables and methods is very important, but this is a story for another day. If you see it or get on variables otherwise write a comment - not many resources available on this topic for PHP.

 


views 7159
  1. Add New Comment