Skip to main content

PHP SDK Samples - Bulk Read Operations

Create a Bulk Read Job
              
              
<?php
namespace com\zoho\crm\sample\bulkread;
use com\zoho\crm\api\bulkread\BulkReadOperations;
use com\zoho\crm\api\bulkread\ResponseWrapper;
use com\zoho\crm\api\bulkread\CallBack;
use com\zoho\crm\api\util\Choice;
use com\zoho\crm\api\bulkread\Query;
use com\zoho\crm\api\bulkread\Criteria;
use com\zoho\crm\api\bulkread\ActionWrapper;
use com\zoho\crm\api\bulkread\SuccessResponse;
use com\zoho\crm\api\bulkread\APIException;
use com\zoho\crm\api\bulkread\RequestWrapper;
use com\zoho\crm\api\bulkread\FileBodyWrapper;
class BulkRead
{
    /**
     * Create BulkRead Job
     * This method is used to create a Bulkread job to export records.
     * @param moduleAPIName The API Name of the record's module
     * @throws Exception
     */
    public static function createBulkReadJob(string $moduleAPIName)
    {
        //Get instance of BulkReadOperations Class
        $bulkReadOperations = new BulkReadOperations();
        
        //Get instance of RequestWrapper Class that will contain the request body
        $requestWrapper = new RequestWrapper();
        
        //Get instance of CallBack Class
        $callback = new CallBack();
        
        // To set valid callback URL.
        $callback->setUrl("https://www.example.com/callback");
        
        //To set the HTTP method of the callback URL. The allowed value is post.
        $callback->setMethod(new Choice("post"));
        
        //The Bulkread Job's details is posted to this URL on successful completion / failure of job.
        $requestWrapper->setCallback($callback);
        
        //Get instance of Query Class
        $query = new Query();
        
        //Specifies the API Name of the module to be read.
        $query->setModule($moduleAPIName);
        
        //Specifies the unique ID of the custom view whose records you want to export.
        $query->setCvid("34770610087501");
        
        // List of Field API Names
        $fieldAPINames = array();
        
        array_push($fieldAPINames, "Last_Name");
        
        //Specifies the API Name of the fields to be fetched.
        $query->setFields($fieldAPINames);
        
        //To set page value, By default value is 1.
        $query->setPage(1);
        
        //Get instance of Criteria Class
        $criteria = new Criteria();
        
        // To set API name of a field.
        $criteria->setAPIName("Created_Time");
        
        // To set comparator(eg: equal, greater_than.).
        $criteria->setComparator(new Choice("between"));
        
        $createdTime = array("2020-06-03T17:31:48+05:30", "2020-06-03T17:31:48+05:30");
        
        // To set the value to be compare.
        $criteria->setValue($createdTime);
        
        //To filter the records to be exported.
        $query->setCriteria($criteria);
        
        //To set query JSON object.
        $requestWrapper->setQuery($query);
        
        //Specify the value for this key as "ics" to export all records in the Events module as an ICS file.
        // $requestWrapper->setFileType(new Choice("ics"));
        
        //Call createBulkReadJob method that takes RequestWrapper instance as parameter 
        $response = $bulkReadOperations->createBulkReadJob($requestWrapper);
        
        if($response != null)
        {
            //Get the status code from response
            echo("Status code : " . $response->getStatusCode() . "\n");
            
            //Get object from response
            $actionHandler = $response->getObject();
            
            if($actionHandler instanceof ActionWrapper)
            {
                //Get the received ActionWrapper instance
                $actionWrapper = $actionHandler;
                
                //Get the list of obtained ActionResponse instances
                $actionResponses = $actionWrapper->getData();
                
                foreach ($actionResponses as $actionResponse)
                {
                    //Check if the request is successful
                    if($actionResponse instanceof SuccessResponse)
                    {
                        //Get the received SuccessResponse instance
                        $successResponse = $actionResponse;
                        
                        //Get the Status
                        echo("Status: " . $successResponse->getStatus()->getValue(). "\n");
                        
                        //Get the Code
                        echo("Code: " . $successResponse->getCode()->getValue(). "\n");
                        
                        echo("Details: " );
                        
                        //Get the details map
                        foreach($successResponse->getDetails() as $key => $value)
                        {
                            //Get each value in the map
                            echo($key . " : " );

                            print_r($value);

                            echo("\n");
                        }
                        
                        //Get the Message
                        echo("Message: " . $successResponse->getMessage()->getValue(). "\n");
                    }
                    //Check if the request returned an exception
                    else if($actionResponse instanceof APIException)
                    {
                        //Get the received APIException instance
                        $exception = $actionResponse;
                        
                        //Get the Status
                        echo("Status: " . $exception->getStatus()->getValue(). "\n");
                        
                        //Get the Code
                        echo("Code: " . $exception->getCode()->getValue(). "\n");
                        
                        echo("Details: " );
                        
                        //Get the details map
                        foreach($exception->getDetails() as $key => $value)
                        {
                            //Get each value in the map
                            echo($key . " : " . $value . "\n");
                        }
                        
                        //Get the Message
                        echo("Message: " . $exception->getMessage()->getValue(). "\n");
                    }
                }
            }
            //Check if the request returned an exception
            else if($actionHandler instanceof APIException)
            {
            //Get the received APIException instance
            $exception = $actionHandler;
                        
            //Get the Status
            echo("Status: " . $exception->getStatus()->getValue(). "\n");
            
            //Get the Code
            echo("Code: " . $exception->getCode()->getValue(). "\n");
            
            echo("Details: " );
            
            //Get the details map
            foreach($exception->getDetails() as $key => $value)
            {
                //Get each value in the map
                echo($key . " : " . $value . "\n");
            }
            
            //Get the Message
            echo("Message: " . $exception->getMessage()->getValue(). "\n");
            }
        }
    }
}
 
Get Bulk Read Job Details
Download Result
              
              
<?php
namespace com\zoho\crm\sample\bulkread;
use com\zoho\crm\api\bulkread\BulkReadOperations;
use com\zoho\crm\api\bulkread\ResponseWrapper;
use com\zoho\crm\api\bulkread\CallBack;
use com\zoho\crm\api\util\Choice;
use com\zoho\crm\api\bulkread\Query;
use com\zoho\crm\api\bulkread\Criteria;
use com\zoho\crm\api\bulkread\ActionWrapper;
use com\zoho\crm\api\bulkread\SuccessResponse;
use com\zoho\crm\api\bulkread\APIException;
use com\zoho\crm\api\bulkread\RequestWrapper;
use com\zoho\crm\api\bulkread\FileBodyWrapper;
class BulkRead
{
    /**
     * Download Result
     * This method is used to download the Bulkread job as a CSV or an ICS file (only for the Events module). 
     * @param jobId The unique ID of the Bulkread job.
     * @param destinationFolder The absolute path where downloaded file has to be stored.
     * @throws Exception
     */
    public static function downloadResult(string $jobId, string $destinationFolder)
    {
        //example
        //String jobId = "34770615177002";
        
        //Get instance of BulkReadOperations Class
        $bulkReadOperations = new BulkReadOperations();
        
        //Call downloadResult method that takes jobId as parameters
        $response = $bulkReadOperations->downloadResult($jobId);

        if($response != null)
        {
            //Get the status code from response
            echo("Status code " . $response->getStatusCode() . "\n");

            if(in_array($response->getStatusCode(), array(204, 304)))
            {
                echo($response->getStatusCode() == 204? "No Content\n" : "Not Modified\n");

                return;
            }

            //Get object from response
            $responseHandler = $response->getObject();

            if($responseHandler instanceof FileBodyWrapper)
            {
                //Get the received FileBodyWrapper instance
                $fileBodyWrapper = $responseHandler;

                //Get StreamWrapper instance from the returned FileBodyWrapper instance
                $streamWrapper = $fileBodyWrapper->getFile();
                
                //Create a file instance with the absolute_file_path
                $fp = fopen($destinationFolder."/".$streamWrapper->getName(), "w");
                
                //Get stream from the response
                $stream = $streamWrapper->getStream();

                fputs($fp, $stream);

                fclose($fp);    
            }
            //Check if the request returned an exception
            else if($responseHandler instanceof APIException)
            {
                //Get the received APIException instance
                $exception = $responseHandler;
                
                //Get the Status
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                
                //Get the Code
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                
                    //Get the details map
                    foreach ($exception->getDetails() as $keyName => $keyValue) 
                    {
                        //Get each value in the map
                        echo($keyName . ": " . $keyValue . "\n");
                    }    
                }
                
                //Get the Message
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}
}