Skip to main content
added 21 characters in body
Source Link
djoo
  • 157
  • 7

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

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

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

Source Link
djoo
  • 157
  • 7

Best way to include and use Zoho SDK in a wordpress plugin

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

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