0

I'm trying to create a plugin that gonna let me use the Zoho API via the Zoho PHP SDK.

So i created a WP Plugin called connect-zoho and enable it. Until here it's easy :)

Then, by CLI i went in my plugin and did :

composer requiere zohocrm/php-sdk

This installed the vendor folder with the content of zoho php sdk.

Now i try to use this SDK inside my plugin wordpress.

In my plugin i created a file zoho-initialize-class.php (copy paste from the github doc) :

<?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);
    }
}
?>

And then i try to call the function Initialize since my plugin file connect-zoho.php like this:

require_once('zoho-initialize-class.php');
ZohoInitialize::initialize();

But i have a problem of namespace and this error :

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

How can i use the SDK inside the plugin ?

Thanks

2
  • Have you required composer's autoloader?
    – kero
    Commented Jan 20, 2021 at 15:44
  • Not specially, I just thought was the best way to do it. And make the update of the SDK easier as well. What do you think behind of this ?
    – djoo
    Commented Jan 20, 2021 at 16:02

1 Answer 1

1

You added the library via Composer, but as it sounds you have not loaded Composer's autoloader in your PHP code. This is necessary so PHP knows where to find classes (without the need for manual require() statements for each file).

If you check the documentation, you'll see this code

require __DIR__ . '/vendor/autoload.php';

This is what you should do in your main plugin file as one of the first steps (definitely before you use any of the classes).

So the final plugin file will look something like this

<?php

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

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

// rest of your code
1
  • Thanks a lot @kero
    – djoo
    Commented Jan 20, 2021 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.