tags:

views:

765

answers:

4

Hi, I'm messing around with REST in PHP and I understand that I can capture the request method vi a $_SERVER['REQUEST_METHOD']. But how do I trigger PUT/DELETE requests in PHP without the use of cURL? I want to use the features of a default PHP installation to do this. I can't imagine changing the method attribute of the form tag to specify a method other than GET or POST would work on all browsers...plus the HTML standard doesn't recognize it.

I'm on PHP 5.2.6, by the way.

Thanks.

+1  A: 

fopen will do HTTP, but I believe it will only do GET requests. If you use fsockopen, you'll need to handle the HTTP protocol yourself, generating headers, etc..

You can use HTTPRequest (in the php_http extension) or PEAR.

Update:

If you're really talking about making PUT/DELETE requests to your PHP scripts, rather than from them, this question may be of interest:

http://stackoverflow.com/questions/165779

Tim Sylvester
I was actually looking at HTTPRequest.I'm trying to imagine how I would implement PUT.I would have an HTML form with the data already present in the fields.On submit, I don't understand how I can trigger PUT so that my form handler handles this as PUT.Could you shed some light on this please?Thanks.
Housni
Tim Sylvester
A: 

If you want to use PHP's built-in functions, but not cURL, you're pretty much stuck with fsockopen(), etc.,

or at best, a library built around those functions, like Snoopy

Frank Farmer
A: 

Usually this is done by a hidden form field, and handled in the application (not the webserver, by and large).

<input type="hidden" name="_method" value="put" />

So in this case you'd use a simple set of if-else statements to determine if the _method variable is overridden (validly, of course). I'd use something like:

$method = 'get';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  if(isset($_POST['_method']
     && ($_POST['_method'] == 'PUT' || $_POST['_method'] == 'DELETE')) {
    $method = strtolower($_POST['_method']);
  } else {
    $method = 'post';
  }
}

This would be a simple way to determine the request type for your application or framework.

The Wicked Flea
That's what I came up with too...the way Rails does it. But that heavily relies on convention. If someone editing the template does not add that, REST would not work.But that seems like the only solution I could find too.Say it isn't so :)
Housni
No, no! Put this kind of code into the *framework* and make it accessible to the code: this way the view doesn't do a thing, and the method is already determined.
The Wicked Flea
A: 

You may want to take a look at WebDav (PHP WebDav slide show). It's built to PUSH, DELETE, Etc... Of course the server you are targeting needs to accept PUSH's and DELETE's which would mean Apache is using mod_webdav or something similar. Otherwise a lower-level solution will be required (as others have indicated)

ChronoFish