2016-09-13 36 views
3

在我的服務器上,我運行了幾個PHP文件來讀取我的Firebase實時數據庫。根據Firebase's documents我需要設置自定義令牌讓我的Firebase PHP Client運行。 Firebase文件說我需要退回此項;PHP Firebase幫助 - 設置JWT

return JWT::encode($payload, $private_key, "RS256"); 

我到底該如何引用JWT類?我下載了一個JWT庫,但我不確定如何將其實施到我的項目中。任何幫助都會很棒,我主要是一名移動開發人員,並且幾乎沒有使用PHP的經驗。

+0

您使用的作曲家? – sonam

回答

8

firebase/php-jwt庫使用作曲家。如果你來自android開發背景,Composer是PHP的依賴管理器,類似於Java中的Maven。您需要知道如何使用php的require/include函數導入php中的類。你需要一些使用PHP的經驗來使用作曲家。

爲了使用火力/ PHP-JWT庫不作曲家,你可以使用下面的示例代碼:(我下載裏面「智威湯遜」文件夾中的庫)

<?php 

require_once 'jwt/src/BeforeValidException.php'; 
require_once 'jwt/src/ExpiredException.php'; 
require_once 'jwt/src/SignatureInvalidException.php'; 
require_once 'jwt/src/JWT.php'; 


use \Firebase\JWT\JWT; 

$key = "example_key"; 
$token = array(
    "iss" => "http://example.org", 
    "aud" => "http://example.com", 
    "iat" => 1356999524, 
    "nbf" => 1357000000 
); 

/** 
* IMPORTANT: 
* You must specify supported algorithms for your application. See 
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 
* for a list of spec-compliant algorithms. 
*/ 
$jwt = JWT::encode($token, $key); 
$decoded = JWT::decode($jwt, $key, array('HS256')); 

print_r($decoded); 

/* 
NOTE: This will now be an object instead of an associative array. To get 
an associative array, you will need to cast it as such: 
*/ 

$decoded_array = (array) $decoded; 

/** 
* You can add a leeway to account for when there is a clock skew times between 
* the signing and verifying servers. It is recommended that this leeway should 
* not be bigger than a few minutes. 
* 
* Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef 
*/ 
    JWT::$leeway = 60; // $leeway in seconds 
    $decoded = JWT::decode($jwt, $key, array('HS256')); 
?> 
+0

謝謝@sonam你保存我的一天.. – Manish