【问题标题】:Flight PHP RESTful API not returning HTTP Status header飞行 PHP RESTful API 不返回 HTTP 状态标头
【发布时间】:2017-04-12 06:06:01
【问题描述】:

我正在使用 Flight PHP 框架在 PHP 中开发 RESTful API。一切正常,除了我的所有路由,来自 API 的响应始终包含 HTTP 状态“200”,即使我使用 PHP 代码将其设置为“403”或“500”:

header('HTTP/1.1 403 Forbidden');

我正在使用 POSTMAN chrome add on 向 API 发送调用,它总是返回状态“200 OK”。

这是 FLIGHT PHP 代码:

Flight::route('GET /organisation/id', function(){

if (isset($_SERVER['HTTP_APIKEY']) && isset($_SERVER['HTTP_CLIENTID'])) {

    $organisationID = checkAPIKey($_SERVER['HTTP_APIKEY']);

    if ($organisationID !== false) {

        $response = array('status' => '200', 'data' => array('organisationID' => $organisationID));
        header('HTTP/1.1 200 OK');
        header('Content-type: application/json');

        logAPICall($_SERVER['HTTP_CLIENTID'], $organisationID, $_SERVER['REMOTE_ADDR'], json_encode($response), '', $_SERVER['HTTP_APIKEY']);

        echo json_encode($response);

    } else {

        header('HTTP/1.1 403 Forbidden');
        header('Content-type: application/json');
        $responseArray = array( 'status' => '403', 'errorCode' => '1', 'error' => 'Unauthorised API access');

        logAPICall($_SERVER['HTTP_CLIENTID'], $organisationID, $_SERVER['REMOTE_ADDR'], json_encode($responseArray), '', 'No API Key');

        $stmt = null;
        $db = null;

        echo json_encode($responseArray);

    }

} else {

    header('HTTP/1.1 403 Forbidden');
    header('Content-type: application/json');
    $responseArray = array( 'status' => '403', 'errorCode' => '1', 'error' => 'Unauthorised API access or Missing Client Header');

    logAPICall('No Client Header', '', $_SERVER['REMOTE_ADDR'], json_encode($responseArray), '', 'No API Key');

    $stmt = null;
    $db = null;

    echo json_encode($responseArray);

}

});

任何关于为什么会发生这种情况的想法将不胜感激!干杯

【问题讨论】:

    标签: php rest flightphp


    【解决方案1】:

    无法回答您的问题,但是您是否尝试过使用框架的 json 方法?它对我有用。

        Flight::json(array(
           'status' => 403,
           'errorCode' => '1'
        ), 403);
    

    source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多