如何启用和使用HTTP PUT和的Apache2和PHP删除? [英] How to enable and use HTTP PUT and DELETE with Apache2 and PHP?

查看:826
本文介绍了如何启用和使用HTTP PUT和的Apache2和PHP删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是如此简单。我跟踪着每一个教程和论坛上,我能找到,但我不能让它工作。我只是想建立在PHP一个RESTful API上的Apache2。

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.

在我的VirtualHost指令我说:

In my VirtualHost directive I say:

<Directory />
    AllowOverride All
    <Limit GET HEAD POST PUT DELETE OPTIONS>
        Order Allow,Deny
        Allow from all
    </Limit>
</Directory>

然而,每一个PUT请求我做给服务器,我得到不支持405的方法。

Yet every PUT request I make to the server, I get 405 method not supported.

使用脚本指令有人主张,但因为我使用mod_php的,而不是CGI,我不明白为什么会工作。

Someone advocated using the Script directive, but since I use mod_php, as opposed to CGI, I don't see why that would work.

人们一提到使用WebDAV,但对我来说,这似乎有点小题大做。毕竟,我不需要DAV锁定,一个DAV文件系统等。所有我想要做的就是传递给PHP脚本的请求,并处理一切自己。我只想让PUT和DELETE的清洁语义。

People mention using WebDAV, but to me that seems like overkill. After all, I don't need DAV locking, a DAV filesystem, etc. All I want to do is pass the request on to a PHP script and handle everything myself. I only want to enable PUT and DELETE for the clean semantics.

推荐答案

您不需要任何配置。只要确保该请求映射到与路径信息你的​​PHP文件和使用的要求。例如,如果你在根一个名为 handler.php 与此内容:

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));

下面HTTP请求将工作:

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.

您可以隐藏PHP扩展名与多视图或可以让网址完全合乎逻辑与 mod_rewrite的

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

又见了 的AcceptPathInfo指令的文件,并就如何<这个问题A HREF =htt​​p://stackoverflow.com/questions/1361673>使PHP无法分析POST数据时ENCTYPE为的multipart / form-data的

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.

这篇关于如何启用和使用HTTP PUT和的Apache2和PHP删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆