2014-11-07 20:59
浏览 69

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

I created a PHP RESTful API following this example (code at the bottom). From what I understand, the API converts non-existing URI components to GET parameters in the address via the .htaccess file like this:

<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]

So if I do, e.g.:


endpoint1/param1 can be parsed in the API implementation to call a PHP function endpoint1(param1).

Now my issue is, param1 is really long, and I want use AJAX to POST param1, but the POST doesn't go through

For example, for the RESTful API:


I POSTed data using AJAX as follows:


In API.class.php, $_SERVER['REQEUEST_METHOD'] is 'POST', but $_POST is empty array.

My questions are:

What is actually happening when I POST to a virtual URL to be handled by mod_rewrite. Is the end result a GET request by mode_rewrite or a POST request by my AJAX call?

How can I modify the code to get the POSTed data through? (Is it possible to ask mod_rewrite to use the POST method instead?)

I am confused here, any pointers are appreciated.

The interface for the relevant REST api is (API.class.php, please see the example for complete code):

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 
        case 'GET':
            $this->request = $this->_cleanInputs($_GET);
        case 'PUT':
            $this->request = $this->_cleanInputs($_GET);
            $this->file = file_get_contents("php://input");
            $this->_response('Invalid Method', 405);
    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条回答 默认 最新

相关推荐 更多相似问题