2015-04-20 191 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

相關問題