在PHP中创建Rest API导致重定向错误

时间:2017-02-18 19:41:32

标签: php .htaccess rest api

我正努力在PHP中创建一个合适的RESTful API。我一直在跟踪http://coreymaynard.com/blog/creating-a-restful-api-with-php/的一些步骤,对我来说,看起来我做的一切都完全相同,除非我尝试转到http://localhost/api/v1/example时出现内部服务器错误。

在apache错误日志中,我看到以下内容:

[Sat Feb 18 19:30:10.594193 2017] [core:error] [pid 10272:tid 1144] [client ::1:58203] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3747): [client ::1:58203] AH00121: r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/api.php
[Sat Feb 18 19:30:10.594193 2017] [core:debug] [pid 10272:tid 1144] core.c(3753): [client ::1:58203] AH00122: redirected from r->uri = /api/v1/example

我的BaseAPI类(他们在示例中称之为API)如下:

abstract class API
{
    protected $method = '';
    protected $endpoint = '';
    protected $verb = '';
    protected $args = Array();

    public function __construct($request)
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Method: *");
        header("Content-Type: application/json");

        $this->args = explode('/', rtrim($request, '/'));
        $this->endpoint = array_shift($this->args);
        if (array_key_exists(0, $this->args) && !is_numeric($this->args[0]))
        {
            $this->verb = 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);
                break;
            case 'GET':
                $this->request = $this->cleanInputs($_GET);
                break;
            case 'PUT':
                $this->request = $this->clearInputs($_GET);
                $this->file = file_get_contents("php://input");
                break;
            default:
                $this->response('Invalid Method', 405);
                break;
        }
    }

    public function processAPI()
    {
        if (method_exists($this, $this->endpoint))
        {
            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 $key => $value)
            {
                $clean_input[$key] = $this->cleanInputs($value);
            }
        }
        else
        {
            $clean_input = htmlspecialchars($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];
    }
}

MyAPI文件如下:

require_once 'BaseApi.php';

class MyAPI extends API
{
    public function __construct($request)
    {
        parent::__construct($request);
    }

    protected function example()
    {
        if ($this->method === "GET")
        {
            return "Hello to my RESTful API";
        }
        else
        {
            return "Only accepts GET requests";
        }
    }
}

我的api.php如下:

require_once 'MyAPI.php';

try
{
    $api = new MyAPI($_REQUEST['request']);
    echo $api->processAPI();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

我的.htaccess文件如下:

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

对我来说,一切都与该文章完全相同,如果它与我在Windows 10上运行的Wamp服务器有任何区别。

1 个答案:

答案 0 :(得分:1)

我怀疑,您在同一个基本目录中拥有所有文件.htaccess*.php

如果是这种情况,替换api/v1/api.php将被视为对不存在文件的另一个请求,因为api.php位于当前目录中而不在api/v1中。

因此,api/v1/example将被重写为api/v1/api.php?request=example,然后重写为api/v1/api.php?request=api.php,然后重写为api/v1/api.php?request=api.php,依此类推。

要使其工作,您必须在基目录中包含.htaccess,并在子目录api/v1中包含所有PHP文件。

如果要将文件保存在一个目录中,重写规则的目标必须指向现有的脚本文件api.php?request=$1,例如

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