duanletao9487 2017-04-10 00:25
浏览 62
已采纳

Flight PHP RESTful API不返回HTTP状态标头

I am using the Flight PHP Framework to develop a RESTful API in PHP. Everything is working perfect except for all my routes the response from the API always contains the HTTP Status '200' even if I set it as '403' or '500' using the PHP code:

header('HTTP/1.1 403 Forbidden');

I am using POSTMAN chrome add on to send calls to the API and it always returns status '200 OK'.

enter image description here

This is the FLIGHT PHP code:

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

}

});

Any ideas as to why this is happening would be much appreciated! Cheers

  • 写回答

1条回答 默认 最新

  • douxian6008 2017-04-11 22:06
    关注

    Can't answer your question, but have you tried using the framework's json method? It works for me.

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

    See source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费