doupo5861 2014-11-12 21:26
浏览 36

无法在REST服务器上获取请求标头

Im using Slim Framework as an API. Im making a GET request to it. But I cannot get the headers. My PHP file looks like:

    require 'Slim/Slim.php';
    $app = new Slim();
    $app->get('/project/:id', add_authorize(), 'getProject');
    $app->run();
    function getProject($project_title) {
        //connect to database and return project details
    }

    function add_authorize() {
        return function (){
        $app = Slim::getInstance();     
        $charset = $app->request->headers->get('ACCEPT_CHARSET');
        };
    }

I get the response:

( ! ) Fatal error: Cannot access protected property Slim::$request in C:\wamp\www\server\device_api\index.php on line 23
Call Stack
#   Time    Memory  Function    Location
1   0.0010  248528  {main}( )   ..\index.php:0
2   0.0240  860056  Slim->run( )    ..\index.php:9
3   0.0240  878816  Slim_Route->dispatch( ) ..\Slim.php:1052
4   0.0240  878864  call_user_func:{C:\wamp\www\server\device_api\Slim\Route.php:387} ( )   ..\Route.php:387
5   0.0240  878944  {closure:C:\wamp\www\server\device_api\index.php:19-25}( )  ..\Route.php:387

I have tried absolutely everything but to no avail. I also tried getting headers directly with:

$device_id = $_SERVER['HTTP_DEVICE_ID'];

but this also failed. Any ideas how I can get a header?

  • 写回答

3条回答 默认 最新

  • dongya0914 2014-11-12 22:05
    关注

    I suggest to do the following and not wrap things in a closure:

    require 'Slim/Slim.php';
    $app = new Slim();
    $app->get('/project/:id', add_authorize(), 'getProject');
    $app->run();
    function getProject($project_title) {
        //connect to database and return project details
    }
    
    function add_authorize() use ($app) {
        $charset = $app->request->headers->get('ACCEPT_CHARSET');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条