How to enable and use HTTP PUT and DELETE with Apache2 and PHP?

后端 未结 6 380
别跟我提以往
别跟我提以往 2020-11-29 04:55

It should be so simple. I\'ve followed every tutorial and forum I could find, yet I can\'t get it to work. I simply want to build a RESTful API in PHP on Apache2.

In

相关标签:
6条回答
  • 2020-11-29 05:02

    You can just post the file name to delete to delete.php on the server, which can easily unlink() the file.

    0 讨论(0)
  • 2020-11-29 05:05

    IIRC the purpose of the form method attribute was to define different transport methods. Consequently, HTML 5.2 only defines GET, POST, and DIALOG methods for transport and dialog action, not how the server should process the data.

    Ruby-on-rails solves this problem by using POST/GET for everything and adding a hidden form variable that defines the actual ReST method. This approach is more clumsy and error-prone, but does remove the burden from both the HTML standard and browser developers.

    The form method was defined before ReST, so you cannot define ReST in HTML, even after enabling Apache and PHP because the browsers conform to HTML and therefore default to GET/POST for all non-HTML defined values. That means, when you send a form to the browser with a PUT method, the browser changes that to GET and uses that instead. The hidden variable, however, passes through everything unchanged, so you can use that to customise your form handling process.

    Hope that helps

    0 讨论(0)
  • 2020-11-29 05:10

    On linux, /etc/apache2/mods-enabled/php5.conf dans php5.load exists. If not, enables this modules (may require to sudo apt-get install libapache2-mod-php5).

    0 讨论(0)
  • 2020-11-29 05:13

    You don't need to configure anything. Just make sure that the requests map to your PHP file and use requests with path info. For example, if you have in the root a file named handler.php with this content:

    <?php
    
    var_dump($_SERVER['REQUEST_METHOD']);
    var_dump($_SERVER['REQUEST_URI']);
    var_dump($_SERVER['PATH_INFO']);
    
    if (($stream = fopen('php://input', "r")) !== FALSE)
        var_dump(stream_get_contents($stream));
    

    The following HTTP request would work:

    Established connection with 127.0.0.1 on port 81
    PUT /handler.php/bla/foo HTTP/1.1
    Host: localhost:81
    Content-length: 5
     
    boo
    HTTP/1.1 200 OK
    Date: Sat, 29 May 2010 16:00:20 GMT
    Server: Apache/2.2.13 (Win32) PHP/5.3.0
    X-Powered-By: PHP/5.3.0
    Content-Length: 89
    Content-Type: text/html
     
    string(3) "PUT"
    string(20) "/handler.php/bla/foo"
    string(8) "/bla/foo"
    string(5) "boo
    "
    Connection closed remotely.
    

    You can hide the "php" extension with MultiViews or you can make URLs completely logical with mod_rewrite.

    See also the documentation for the AcceptPathInfo directive and this question on how to make PHP not parse POST data when enctype is multipart/form-data.

    0 讨论(0)
  • 2020-11-29 05:13

    The technical limitations with using PUT and DELETE requests does not lie with PHP or Apache2; it is instead on the burden of the browser to sent those types of requests.

    Simply putting <form action="" method="PUT"> will not work because there are no browsers that support that method (and they would simply default to GET, treating PUT the same as it would treat gibberish like FDSFGS). Sadly those HTTP verbs are limited to the realm of non-desktop application browsers (ie: web service consumers).

    0 讨论(0)
  • 2020-11-29 05:21

    AllowOverride AuthConfig

    try this. Authentication may be the problem. I was working with a CGI script written in C++, and faced some authentication issues when passed DELETE. The above solution helped me. It may help in your case too.


    Also even if you don't get the solution for your problem of PUT and DELETE, do not stop working rather use "CORS". It is a google chrome app, which will help you bypass the problem, but remember it is a temporary solution, so that your work or experiments doesn't remain freeze for long. Obviously, you cannot ask your client to have "CORS" enabled to run your solution, as it may compromise systems security.

    0 讨论(0)
提交回复
热议问题