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');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)