使用apache mod_rewrite实现的PHP POST到RESTful API

时间:2014-11-07 20:59:56

标签: php ajax .htaccess mod-rewrite

我在this example之后创建了一个PHP RESTful API(底部的代码)。根据我的理解,API通过.htaccess文件将不存在的URI组件转换为地址中的GET参数,如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>

如果我这样做,例如:

http://mysite/api/v1/endpoint1/param1
可以在API实现中解析

endpoint1/param1以调用PHP函数endpoint1(param1)

现在我的问题是,param1非常长,我希望将AJAX用于POST param1,但POST不会通过

例如,对于RESTful API:

http://mysite/api/v1/endpoint1

我使用AJAX发布了data,如下所示:

$.post('http://mysite/api/v1/endpoint1/',data,callback,'json');

API.class.php中,$_SERVER['REQEUEST_METHOD']'POST',但$_POST为空数组。

我的问题是:

当我POSTmod_rewrite处理虚拟网址时,实际发生了什么。最终结果是GET请求mode_rewrite请求或我的AJAX调用请求POST吗?

如何修改代码以获取POST ed数据? (是否可以要求mod_rewrite使用POST方法?)

我很困惑,任何指针都很受欢迎。


相关REST api的界面是(API.class.php,有关完整代码,请参阅the example):

<?php
abstract class API
{
    protected $method = '';
    protected $endpoint = '';
    protected $args = Array();
    protected $file = Null;
    public function __construct($request) {
        header("Access-Control-Allow-Orgin: *");
        header("Access-Control-Allow-Methods: *");
        header("Content-Type: application/json");
        $this->args = explode('/', rtrim($request, '/'));
        $this->endpoint = array_shift($this->args);
        $this->method = $_SERVER['REQUEST_METHOD'];
        if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
            if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
                $this->method = 'DELETE';
            } else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
                $this->method = 'PUT';
            } else {
                throw new Exception("Unexpected Header");
            }
        }
        switch($this->method) {
        case 'DELETE':
        case 'POST':
            $this->request = $this->_cleanInputs($_POST);
            $this->args[] = ???; //Problem line: how do I add post to the args 
            break;
        case 'GET':
            $this->request = $this->_cleanInputs($_GET);
            break;
        case 'PUT':
            $this->request = $this->_cleanInputs($_GET);
            $this->file = file_get_contents("php://input");
            break;
        default:
            $this->_response('Invalid Method', 405);
            break;
        }
    }
    public function processAPI() {
        if ((int)method_exists($this, $this->endpoint) > 0) {
            return $this->_response($this->{$this->endpoint}($this->args));
        }
        return $this->_response("No Endpoint: $this->endpoint", 404);
    }
    private function _response($data, $status = 200) {
        header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
        return json_encode($data);
    }
    private function _cleanInputs($data) {
        $clean_input = Array();
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $clean_input[$k] = $this->_cleanInputs($v);
            }
        } else {
            $clean_input = trim(strip_tags($data));
        }
        return $clean_input;
    }
    private function _requestStatus($code) {
        $status = array(  
            200 => 'OK',
            404 => 'Not Found',   
            405 => 'Method Not Allowed',
            500 => 'Internal Server Error',
        ); 
        return ($status[$code])?$status[$code]:$status[500]; 
    }
} 
?>

1 个答案:

答案 0 :(得分:0)

我相信这是你遇到的问题: Is it possible to redirect post data?

您应该在.htaccess文件中更改此行: RewriteRule api / v1 /(.*)$ api / v1 / api.php?request = $ 1 [QSA,NC,L]

于: RewriteRule api / v1 /(.*)$ api / v1 / api.php?request = $ 1 [QSA,NC,P]