2015-04-20 136 views
2

我正在使用瘦身框架来创建宁静的api。瘦身框架无法正常工作的身份验证

我能创造一个获取API,但是当我添加认证的GET请求时,它抛出一个错误(高级REST客户端,谷歌的Chrome扩展程序),这里是错误:

<html><head><title>Slim Application Error</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body><h1>Slim Application Error</h1><p>The application could not run because of the following error:</p><h2>Details</h2><div><strong>Type:</strong> ErrorException</div><div><strong>Code:</strong> 8</div><div><strong>Message:</strong> Undefined variable: apiKey</div><div><strong>File:</strong> /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php</div><div><strong>Line:</strong> 34</div><h2>Trace</h2><pre>#0 /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php(34): Slim\Slim::handleErrors(8, 'Undefined varia...', '/Library/WebSer...', 34, Array) 
#1 [internal function]: authenticate(Object(Slim\Route)) 
#2 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Route.php(433): call_user_func_array('authenticate', Array) 
#3 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Slim.php(1307): Slim\Route->dispatch() 
#4 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/Flash.php(85): Slim\Slim->call() 
#5 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call() 
#6 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call() 
#7 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Slim.php(1254): Slim\Middleware\PrettyExceptions->call() 
#8 /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php(100): Slim\Slim->run() 
#9 {main}</pre></body></html> 

这里是我的代码:

<?php 

require '.././libs/Slim/Slim.php'; 

\Slim\Slim::registerAutoloader(); 

$app = new \Slim\Slim(); 

function isValidApiKey($api_key) { 
    $m = new MongoClient(); 
    $db = $m->pascal; 
    $collection = $db->apiUsers; 
    if($collection->findOne(array('apiKey' => $api_key))){ 
     return true; 
    } 

    else{ 
     return false; 
    } 

} 

function authenticate(\Slim\Route $route) { 
    // Getting request headers 
    $headers = apache_request_headers(); 
    $response = array(); 
    $app = \Slim\Slim::getInstance(); 

    // Verifying Authorization Header 
    if (isset($headers['Authorization'])) { 

     // get the api key 
     $api_key = $headers['Authorization']; 
     echo $apiKey; 
     // validating api key 
     //$db = new dbSupport(); 
     if ($isValidApiKey($api_key) === false) { 

      // api key is not present 
      $response["error"] = true; 
      $response["message"] = "Access Denied. Invalid Api key"; 
      echoRespnse(401, $response); 
      $app->halt(401); 

     } 
     else{ 

     } 
    } else { 
     // api key is missing in header 
     $response["error"] = true; 
     $response["message"] = "Api key is misssing"; 
     echoRespnse(400, $response); 
     $app->halt(401); 
    } 
} 


$app->get('/offerData','authenticate',function() use ($app) { 

      $m = new MongoClient(); 
      $db = $m->pascal; 
      $collection = $db->offerDetails; 

      $offer_array = array(); 
      $cursor = $collection->find(); 
      $offer_array["offers"] = array(); 

      foreach ($cursor as $document) { 

       $offerData = array('title' => $document['title'], 
          'discription' => $document['discription'], 
          'create_time' => $document['create_time'], 
          'expire_time' => $document['expire_time'], 
          'coordinates' => $document['loc']['coordinates'], 
          'address' => $document['address'], 
          'tags' => $document['tags'], 
          'phone_number' => $document['phone_number'], 
          'email' => $document['email'], 
          'website' => $document['website'], 
          'img_url' => $document['img_url'] 
          ); 
       array_push($offer_array["offers"], $offerData); 
      } 
      $offer_array["error"] = false; 
      echoRespnse(200, $offer_array); 
     }); 

function echoRespnse($status_code, $response) { 
    $app = \Slim\Slim::getInstance(); 
    // Http response code 
    $app->status($status_code); 

    // setting response content type to json 
    $app->contentType('application/json'); 

    echo json_encode($response); 
} 

$app->run(); 
?> 

任何想法什么导致这个错误? 谢谢

回答

1

请在$标头上运行print_r命令,看看你在那里得到了什么。我认为关键是没有设置或者它不来在所有代码

好吧,那么第28行之前:

$api_key=null ; 

一次试试这个,让我知道。

这是错误:

  $api_key = $headers['Authorization']; 
      echo $apiKey; 

但回声印刷错误的变量。它应该是

  echo $api_key; 
+0

我得到的,除了以前的错误以下的输出: '阵列 ( [Host] => localhost [Connection] => keep-alive [User-Agent] => Mozilla/5.0(Macintosh; Intel Mac OS X 10_10_2)AppleWebKit/537.36(KHTML,li柯壁虎)铬/ 42.0.2311.90 Safari浏览器/ 537.36 [授权] => b2c58c89674ffa83ba9bc6b0cc5bbeda [接受] => */* [DNT] => 1 [接受编码] => gzip的,放气,SDCH [接受-Language] => en-US,en; q = 0.8 )' – digiVader

+0

没有。它仍然显示相同的错误。 我认为错误出现在'if(isset($ headers ['Authorization'])){'line,becoz no break statements after after。 – digiVader

+0

发现错误,你指出的是ture,但那不是导致错误的东西... 这是'if($ isValidApiKey($ api_key)=== false)' isValidApiKey()is一个函数,这是一个愚蠢的错误。 反正谢谢你的帮助。 – digiVader

相关问题