hello friends!
how do i get the PUT arguments inside the Drupal PHP code?
when i say PUT i mean the HTTP method PUT, like the more common POST method.
i'm trying to work up the RESTAPI module which was probably never even tested or used, because ALL the PUT methods don't work.
so can anyone help me and tell me how it's done?

10x alot!

rocky

Comments

tomchuk’s picture

It requires some apache configuration. Make sure mod_actions is enabled, and add a "Script PUT /index.php" in your virtual host or directory container (or at the server level):

<VirtualHost *:80>
  ServerName foo.com
  Script PUT /index.php
  DocumentRoot /var/www/foo
  <Directory />
    AllowOverride All
  </Directory>
</VirtualHost>

For your module. you probably have something like this already, but here's how you'd see what is PUT to the path /rest:

function restapi_menu($may_cache){
  $items[] = array();
  if ($may_cache){
    $items[] = array(
      'path' => 'rest',
      'title' => 'rest',
      'callback' => 'restapi_handle',
      'access' => TRUE,
      'type' => MENU_CALLBACK
      );
  }
  return $items;
}

function restapi_handle(){
  if ($_SERVER['REQUEST_METHOD'] == 'PUT'){
    // Parse PUT data
    $putdata = array();
    parse_str(file_get_contents("php://input"), $putdata);

    // Set respose header according to section 9.6 of rfc2616
    drupal_set_header('HTTP/1.1 201 Created');
    drupal_set_header('Content-type: text/plain');

    // Do something interesting with PUT data
    print_r($putdata);

    // The client probably doesn't care about anything that would be returned (besides the header), so just exit
    module_invoke_all('exit');
    exit;
  }
}

To test:

curl -X PUT http://foo.com/rest -d type=foo -d bar=baz

will result in:

Array
(
    [type] => foo
    [bar] => baz
)