【问题标题】:PHP POST to RESTful API implemented with apache mod_rewrite使用 apache mod_rewrite 实现 PHP POST 到 RESTful API
【发布时间】:2014-11-08 05:06:59
【问题描述】:

我在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

endpoint1/param1可以在API实现中解析,调用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 是空数组。

我的问题是:

当我POST 到一个由mod_rewrite 处理的虚拟 URL 时实际发生了什么。最终结果是mode_rewriteGET 请求还是我的AJAX 调用的POST 请求?

如何修改代码以获取POSTed 数据? (是否可以要求 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]; 
    }
} 
?>

【问题讨论】:

    标签: php ajax .htaccess mod-rewrite


    【解决方案1】:

    我相信这是您遇到的问题: 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]

    【讨论】:

    • 感谢您的帮助。但是在更改为 [QSA, NC, P] 之后,[QSA](名称捕获??)部分似乎停止工作。现在输入http://mysite/api/v1/a 会得到The requested URL /var/www.../api/v1/api.php was not found on this server
    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2023-03-15
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多