我使用Slim和bshaffer的OAuth2.0库来构建API。目前,我正在尝试扩展OAuth库中包含的Pdo.php类以覆盖一些函数。但是,当我尝试运行我的代码时,我收到的错误是:
PHP致命错误:界面
'的OAuth2 \存储\ AuthorizationCodeInterface'没找到
/vendor/bshaffer/oauth2-server-php/src/OAuth2/Storage/Pdo.php上线 21
由于这是基类,我不想修改它,我不知道如何解决这个问题。
另外,使用像这样的基类Pdo类工作正常,它能够找到接口
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $user, 'password' => $pw));
这是我的index.php文件
<?php
use League\OAuth2\Server\Storage\SessionInterface;
require '/***/root/vendor/Slim/Slim.php';
require_once('/***/root/vendor/bshaffer/oauth2-server-php/src/OAuth2/Autoloader.php');
require_once('/***/root/vendor/bshaffer/oauth2-server-php/src/OAuth2/Storage/Pdo.php');
require_once('/***/root/custom_pdo.php');
OAuth2\Autoloader::register();
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->post(
'/token',
function () {
$dsn = 'mysql:host='.MYSQL_HOST.';dbname=DB';
$user = MYSQL_USER;
$pw = MYSQL_PASS;
$storage = new custom_pdo(array('dsn' => $dsn, 'username' => $user, 'password' => $pw));
$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
$response = $server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
print_r($response);
}
);
$app->run();
这是我的custom_pdo.php类
<?php
namespace OAuth2\Storage;
use OAuth2\OpenID\Storage\UserClaimsInterface;
use OAuth2\OpenID\Storage\AuthorizationCodeInterface as OpenIDAuthorizationCodeInterface;
require_once('/***/root/vendor/bshaffer/oauth2-server-php/src/OAuth2/Autoloader.php');
require_once('/***/root/vendor/bshaffer/oauth2-server-php/src/OAuth2/Storage/AuthorizationCodeInterface.php');
class custom_pdo extends Pdo {
}
Bshaffer Pdo课程:https://github.com/bshaffer/oauth2-server-php/blob/develop/src/OAuth2/Storage/Pdo.php
答案 0 :(得分:0)
看起来这是命名空间问题。我没有用命名空间调用我的create object语句,这导致了我遇到的各种问题。这是最终的代码:
的index.php
<?php
use League\OAuth2\Server\Storage\SessionInterface;
require '/***/root/vendor/autoload.php';
require "/***/root/custom_pdo.php";
$app = new \Slim\Slim();
$app->post(
'/token',
function () {
$dsn = 'mysql:host='.MYSQL_HOST.';dbname=DB';
$user = MYSQL_USER;
$pw = MYSQL_PASS;
$storage = new OAuth2\Storage\custom_pdo(array('dsn' => $dsn, 'username' => $user, 'password' => $pw));
$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
$response = $server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
print_r($response);
}
);
$app->run();
custom.php
<?php
namespace OAuth2\Storage;
use OAuth2\OpenID\Storage\UserClaimsInterface;
use OAuth2\OpenID\Storage\AuthorizationCodeInterface as OpenIDAuthorizationCodeInterface;
class custom_pdo extends Pdo {
}