Php Luracast-Restler认证

Php Luracast-Restler认证,php,api,restful-authentication,Php,Api,Restful Authentication,我正在使用Luracast restler,并试图通过实现IAAuthenticate接口来实现一些身份验证 问题是,我的身份验证代码需要查询我的数据库以检索用户私钥。此私钥将始终在url请求中提供(哈希) 我只想为每个请求打开一个数据库连接,因此我需要将db connection变量传递给实现IAAuthenticate的类和处理所有请求的其他类。但我不知道如何将变量传递给实现IAAuthenticate的类 可能吗 供参考,这里有 thks。为API和身份验证类使用单DB连接 创建一个名为c

我正在使用Luracast restler,并试图通过实现IAAuthenticate接口来实现一些身份验证

问题是,我的身份验证代码需要查询我的数据库以检索用户私钥。此私钥将始终在url请求中提供(哈希)

我只想为每个请求打开一个数据库连接,因此我需要将db connection变量传递给实现IAAuthenticate的类和处理所有请求的其他类。但我不知道如何将变量传递给实现IAAuthenticate的类

可能吗

供参考,这里有

thks。为API和身份验证类使用单DB连接 创建一个名为
config.php
的php文件,将所有数据库信息以及数据库连接和选择放在一起

比如说

<?php
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'mysql_db');
//initalize connection to use everywhere
//including auth class and api classes
mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
您的API类可以有一个查询相同数据库的受保护方法,也可以是使用相同连接返回数据的不同表。为了简单起见,我在这里使用相同的表

<?php
require_once 'config.php';
class Simple {
    function index() {
        return 'public api result';
    }
    protected function restricted() {
        $query = mysql_query("SELECT * FROM login");
        $result = array();
        while ($row = mysql_fetch_assoc($query)) {
            $result[]=$row;
        }
        return $result;
    }
}
以及index.php,其中包含以下内容

<?php
require_once '../../restler/restler.php';

#set autoloader
#do not use spl_autoload_register with out parameter
#it will disable the autoloading of formats
spl_autoload_register('spl_autoload');

$r = new Restler();

$r->addAPIClass('Simple','');
$r->addAuthenticationClass('BasicAuthentication');
$r->handle();
明白了

echo mysql_affected_rows();

此行导致输出为text/html格式。评论出来了,我就可以走了。

嘿,那太酷了!还有一个问题。。。您将如何将上述方法扩展到类似AWS的身份验证()中,该身份验证()包括一个日期头(例如“Date:Thu,14 Aug 2008 17:08:48 GMT”),身份验证信息将根据该日期头进行散列?
<?php
require_once '../../restler/restler.php';

#set autoloader
#do not use spl_autoload_register with out parameter
#it will disable the autoloading of formats
spl_autoload_register('spl_autoload');

$r = new Restler();

$r->addAPIClass('Simple','');
$r->addAuthenticationClass('BasicAuthentication');
$r->handle();
[
  {
    "id": "1",
    "logged": "2011-11-01 22:50:05",
    "user": "arul",
    "pass": "mypass"
  },
  {
    "id": "2",
    "logged": "2011-11-01 23:43:25",
    "user": "paulo",
    "pass": "hispass"
  }
]
echo mysql_affected_rows();