在WordPress插件中包含和使用Zoho SDK的最佳方式

时间:2021-01-20 作者:djoo

我正在尝试创建一个插件,让我可以通过Zoho PHP SDK使用Zoho API。

所以我创建了一个名为connect zoho的WP插件并启用它。在此之前,一切都很简单:)

然后,通过CLI,我进入插件并执行了以下操作:

composer requiere zohocrm/php-sdk
这安装了包含zoho php sdk内容的供应商文件夹。

现在我尝试在我的插件wordpress中使用这个SDK。

在我的插件中,我创建了一个文件zoho initialize类。php(从github文档复制粘贴):

<?php
namespace com\\zoho\\crm\\sample\\initializer;

use com\\zoho\\api\\authenticator\\OAuthToken;

use com\\zoho\\api\\authenticator\\TokenType;

use com\\zoho\\api\\authenticator\\store\\DBStore;

use com\\zoho\\api\\authenticator\\store\\FileStore;

use com\\zoho\\crm\\api\\Initializer;

use com\\zoho\\crm\\api\\UserSignature;

use com\\zoho\\crm\\api\\SDKConfigBuilder;

use com\\zoho\\crm\\api\\dc\\USDataCenter;

use com\\zoho\\api\\logger\\Logger;

use com\\zoho\\api\\logger\\Levels;

class ZohoInitialize
{
    public static function initialize()
    {
        /*
            * Create an instance of Logger Class that takes two parameters
            * 1 -> Level of the log messages to be logged. Can be configured by typing Levels "::" and choose any level from the list displayed.
            * 2 -> Absolute file path, where messages need to be logged.
        */
        $logger = Logger::getInstance(Levels::INFO, "/Users/user_name/Documents/php_sdk_log.log");

        //Create an UserSignature instance that takes user Email as parameter
        $user = new UserSignature("[email protected]");

        /*
            * Configure the environment
            * which is of the pattern Domain.Environment
            * Available Domains: USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter
            * Available Environments: PRODUCTION(), DEVELOPER(), SANDBOX()
        */
        $environment = USDataCenter::PRODUCTION();

        /*
            * Create a Token instance
            * 1 -> OAuth client id.
            * 2 -> OAuth client secret.
            * 3 -> REFRESH/GRANT token.
            * 4 -> Token type(REFRESH/GRANT).
            * 5 -> OAuth redirect URL.
        */
        $token = new OAuthToken("clientId", "clientSecret", "REFRESH/GRANT token", TokenType::REFRESH/GRANT, "redirectURL");

        /*
        * Create an instance of DBStore.
        * 1 -> DataBase host name. Default value "localhost"
        * 2 -> DataBase name. Default  value "zohooauth"
        * 3 -> DataBase user name. Default value "root"
        * 4 -> DataBase password. Default value ""
        * 5 -> DataBase port number. Default value "3306"
        */
        //$tokenstore = new DBStore();

        $tokenstore = new DBStore("hostName", "dataBaseName", "userName", "password", "portNumber");

        // $tokenstore = new FileStore("absolute_file_path");

        $autoRefreshFields = false;

        $pickListValidation = false;

        // Create an instance of SDKConfig
        $sdkConfig = (new SDKConfigBuilder())->setAutoRefreshFields($autoRefreshFields)->setPickListValidation($pickListValidation)->build();

        $resourcePath = "/Users/user_name/Documents/phpsdk-application";

        //Create an instance of RequestProxy
        $requestProxy = new RequestProxy("proxyHost", "proxyPort", "proxyUser", "password");

        /*
          * Call static initialize method of Initializer class that takes the following arguments
          * 1 -> UserSignature instance
          * 2 -> Environment instance
          * 3 -> Token instance
          * 4 -> TokenStore instance
          * 5 -> SDKConfig instance
          * 6 -> resourcePath - A String
          * 7 -> Log instance (optional)
          * 8 -> RequestProxy instance (optional)
        */
        Initializer::initialize($user, $environment, $token, $tokenstore, $sdkConfig, $resourcePath, $logger, $requestProxy);
    }
}
?>
然后我尝试调用函数Initialize,因为我的插件文件connect zoho。php如下所示:

require_once(\'zoho-initialize-class.php\');
ZohoInitialize::initialize();
但我有一个名称空间问题,这个错误:

PHP Fatal error:  Uncaught Error: Class \'com\\zoho\\api\\logger\\Logger\' not found in /Users/user/website/zoho/wp-content/plugins/connect-zoho/zoho-initialize-class.php:32
如何在插件中使用SDK?

谢谢

1 个回复
最合适的回答,由SO网友:kero 整理而成

您通过Composer添加了库,但听起来您还没有在PHP代码中加载Composer的自动加载器。这是必要的,以便PHP知道在哪里可以找到类(无需手动require() 每个文件的语句)。

如果你check the documentation, 您将看到此代码

require __DIR__ . \'/vendor/autoload.php\';
这是您应该在主插件文件中执行的第一步(在使用任何类之前)。

最后的插件文件如下所示

<?php

/**
 * Plugin Name:     ...
 * Plugin URI:      ...
 * ...
 */

require_once __DIR__ . \'/vendor/autoload.php\';

// rest of your code

相关推荐