3

I am currently implementing a RESTful web service which talks XML using CodeIgniter and REST by Phil Sturgeon. I am now stuck at how to read XML from HTTP PUT. This is what I did.

At the client side:

$(function(){
    // Bind a click event to the 'ajax' object id
    $("#new_user").click(function(evt){
        // JavaScript needs totake over. So stop the browser from redirecting the page
        evt.preventDefault();
        var str = '<?xml version="1.0" encoding="UTF-8"?><xml><name>'+$("#txtname").val()+'</name><email>'+$("#txtemail").val()+'</email></xml>';

        // Ajax request to get the data
        $.ajax({
            // URL from the link that was clicked on
            url: $(this).attr("href"),
                        type: "put",
                        contentType: "application/xml",
                        processData: false,
                        data: str,
            success: function(data, textStatus, jqXHR){
                //alert('Successful AJAX request!');
                                   //var items = parseXml(data);
                                   //printXml(items);
            },
            // Failed to load request. This could be caused by any number of problems like server issues, bad links, etc.
            error: function(jqXHR, textStatus, errorThrown){
                alert('Oh no! A problem with the Ajax request!');
            }
        });
    });
});

At the server side:

public function users_put(){
    $input = file_get_contents('php://input');
    print_r($input);
}

It prints out nothing. The above JavaScript code and function works well in HTTP POST.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AmeL
  • 31
  • 1
  • 3
  • I think my problem does not lie in client side's code; but instead at server. because I saw this in developer console of Google Chrome: Request URL:http://test.localhost/index.php/api/usercomp/users/id/1 Request Method:PUT Request Payload fdfdffff So it means, the xml was sent to server successfully. but i just don't know how to read it. – AmeL Jul 25 '11 at 08:52

2 Answers2

2

The manual has a good reference for that: http://php.net/manual/en/features.file-upload.put-method.php

You cannot handle PUT requests without altering the HTTP daemon's setup.


If you're using Apache and have access to mod_rewrite, make a .htaccess file in the root folder that you PUT to with something like:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ handler.php?uri=$1 [L,QSA]

But the details depend on what HTTP daemon (Apache, IIS, lighttpd, etc) and which PHP framework you use.

Tino Didriksen
  • 2,215
  • 18
  • 21
  • do you mean, i need to enable http put in httpd.conf? if you do mean it, well, it still not works in my case. – AmeL Jul 25 '11 at 08:54
  • Read the manual page...it explains how quite well. You need to redirect all PUT requests to a PHP script. – Tino Didriksen Jul 25 '11 at 08:57
  • I don't think i should do it because the framework already redirect all http method to the exact handler defined in the class. – AmeL Jul 25 '11 at 09:00
  • In that case, I'd say use POST instead. PUT will require some mod_rewrite magic at least, and preferably a config change. – Tino Didriksen Jul 25 '11 at 09:04
  • Well, that's quite good idea anyway which i already tested it working. but just a noobie question: so there is no real implementation of http PUT handler in RESTful web service? – AmeL Jul 25 '11 at 09:10
  • http://stackoverflow.com/questions/630453/put-vs-post-in-rest discusses that. Both POST and PUT are used, but PUT takes more configuration to get working with PHP. – Tino Didriksen Jul 25 '11 at 09:21
  • are you sure you already read the link you gave me? it is about the definition of POST, PUT, not implementation of the handler for PUT method in server side. – AmeL Jul 25 '11 at 09:26
  • Please and again please give me the procedure, or script on how to make it work, not something like 'it takes more configuration'....it needs http's daemon's setup....it needs to have a config change,....it requires some mod_rewrite.....Those are not the answer to my question. – AmeL Jul 25 '11 at 09:32
0

Use POST. Your application will have to determine if the request is a 'PUT' or not. If you specify the id of the object that you are modifying then you can assume it is a 'PUT' request. I'm not sure how CodeIgniter handles this, but I do know that Zend Framework automatically routes to the putAction when the id is specified. (e.g. /account/5)

Jason Rice
  • 1,686
  • 1
  • 12
  • 17