在Symfony中使用Mautic api-library时,尝试调用名为"validateAccessToken"的未定义方法

Hug*_*ira 11 php oauth symfony mautic

我正在尝试在我的Symfony项目中使用mautic/api-library.我正在使用Symfony 2.8.9和PHP 5.6.14.

我在composer和autoload.php文件中包含了api-library项目.在我的控制器中,我已经声明了api-library类:

use Mautic\Auth\ApiAuth;
use Mautic\Auth\OAuth;
Run Code Online (Sandbox Code Playgroud)

并尝试从我的mautic安装中获取令牌:

$settings = array(
    'baseUrl'      => 'http://mymauticinstallation.com',
    'version'      => 'OAuth1a',
    'clientKey'    => 'myCLientKey',    
    'clientSecret' => 'mySecretClient',  
    'callback'     => 'https://api.mysymfonyapp.com/'
);
$auth = new ApiAuth();
$auth->newAuth($settings);
if ($auth->validateAccessToken()) {
    if ($auth->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行此代码时,我在控制台中收到此错误:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call an undefined method named "validateAccessToken" of class "Mautic\Auth\ApiAuth"
Run Code Online (Sandbox Code Playgroud)

查看mautic ApiAuth类,newAuth方法通过refection 使用实例化:

public function newAuth($parameters = array(), $authMethod = 'OAuth')
{
    $class      = 'Mautic\\Auth\\'.$authMethod;
    $authObject = new $class();

    ...

    return $authObject;
}
Run Code Online (Sandbox Code Playgroud)

根据异常消息,反射不返回OAuth类实例.有谁知道是什么原因造成的?我已经检查过,我正在满足PHP和Symfony的最低要求.有没有与PHP版本和反射有关的东西?

提前致谢.

lol*_*lmx 1

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call an undefined method named "validateAccessToken" of class "Mautic\Auth\ApiAuth"
Run Code Online (Sandbox Code Playgroud)

意味着该方法validateAccessToken不存在于 中Mautic\Auth\ApiAuth,实际上它不是在那里定义的,而是在 中定义的Mautic\Auth\OAuth

// Mautic\Auth\ApiAuth
public function newAuth($parameters = array(), $authMethod = 'OAuth')
{
    $class      = 'Mautic\\Auth\\'.$authMethod;
    $authObject = new $class();

    ...

    return $authObject; // <-- it returns an object, use it!
}
Run Code Online (Sandbox Code Playgroud)

所以你错过了将返回的对象存储在变量中以使用它

$apiAuth = new ApiAuth();
$auth = $apiAuth->newAuth($settings);
if ($auth->validateAccessToken()) {
    if ($auth->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
    }
}
Run Code Online (Sandbox Code Playgroud)