PHP SDK - Bulk Read API
Use the below code to schedule a bulk read job to export records that match the criteria. This job returns a "job_id" in the response. For more details, refer to the Create a Bulk Read Job page of our API guide.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Create Bulk Read job (Bulk Export) */
public static function createBulkReadJob()
{
$restClient = ZCRMRestClient::getInstance();//to get the rest client
$bulkReadrecordIns = $restClient->getBulkReadInstance("ModuleAPIName", null);//To get the ZCRMBulkRead instance of the module
$group1 = ZCRMBulkCriteria::getInstance();// To get ZCRMCriteria instance
$group1->setGroupOperator("and");// To set criteria group_operator(Supported values - and, or).
$grouplist1 = array();// List of ZCRMCriteria instance
$group2 = ZCRMBulkCriteria::getInstance();
$group2->setGroupOperator("or");
$grouplist2 = array();
$group3 = ZCRMBulkCriteria::getInstance();
$group3->setGroupOperator("and");
$grouplist3 = array();
$group4 = ZCRMBulkCriteria::getInstance();
$group4->setAPIName("Last_Name");// To set API name of a field.
$group4->setComparator("equal");// To set comparator(eg: equal, greater_than.).
$group4->setValue("PHP Test Lead1");// To set the value to be compare.
array_push($grouplist3,$group4);// To add ZCRMCriteria instance.
$group5 = ZCRMBulkCriteria::getInstance();
$group5->setGroupOperator("and");
$grouplist5 = array();
$group6 = ZCRMBulkCriteria::getInstance();
$group6->setAPIName("First_Name");
$group6->setComparator("equal");
$group6->setValue("first_name");
array_push($grouplist5, $group6);
$group5->setGroup($grouplist5);
$group7 = ZCRMBulkCriteria::getInstance();
$group7->setAPIName("Full_Name");
$group7->setComparator("equal");
$group7->setValue("name");
array_push($grouplist5, $group7);
$group5->setGroup($grouplist5);
array_push($grouplist3,$group5);
$group3->setGroup($grouplist3);
array_push($grouplist2,$group3);
$group8 = ZCRMBulkCriteria::getInstance();
$group8->setAPIName("First_Name");
$group8->setComparator("equal");
$group8->setValue("testsdk");
array_push($grouplist2,$group8);
$group9 = ZCRMBulkCriteria::getInstance();
$group9->setAPIName("Company");
$group9->setComparator("equal");
$group9->setValue("ZOHO");
array_push($grouplist2,$group9);
$group2->setGroup($grouplist2);
array_push($grouplist1,$group2);
$group10 = ZCRMBulkCriteria::getInstance();
$group10 ->setAPIName("Phone");
$group10->setComparator("equal");
$group10->setValue("999-999-9999");
array_push($grouplist1,$group10);
$group1->setGroup($grouplist1);
$query = ZCRMBulkQuery::getInstance();
$query->setCvId("3477067501");// To set custom view id of the module
$fields = array();// List of Field API Names
array_push($fields,"First_Name");
array_push($fields,"Last_Name");
array_push($fields,"Email");
$query->setFields($fields);// To set list of API Name of the fields to be fetched.
$query->setPage(1);// To set page value, By default value is 1.
$query->setCriteria($group1);// To set ZCRMCriteria instance.
$bulkReadrecordIns->setQuery($query);// To set ZCRMBulkQuery instance.
$callBack = ZCRMBulkCallBack::GetInstance();// To get ZCRMBulkCallBack instance
$callBack->setUrl("https://www.zoho.com");// To set callback URL.
$callBack->setMethod("post");// To set the HTTP method of the callback url. The allowed value is post.
$bulkReadrecordIns->setCallBack($callBack);// To set ZCRMBulkCallBack instance
// $bulkReadrecordIns->setFileType("ics");// Set the value for this key as "ics" to export all records in the Events module as an ICS file.
$recordres = $bulkReadrecordIns->createBulkReadJob();// To create Bulk read job.
echo "HTTP Status Code:" . $recordres->getHttpStatusCode()."\n"; // To get http response code
echo "Status:" . $recordres->getStatus()."\n"; // To get response status
echo "Message:" . $recordres->getMessage()."\n"; // To get response message
echo "Code:" . $recordres->getCode()."\n"; // To get status code
echo "Details:" . json_encode($recordres->getDetails())."\n";
echo "Response Json".json_encode($recordres->getResponseJSON())."\n";
$readIns = $recordres->getData();// To get the ZCRMBulkRead instance.
echo ($readIns->getCreatedTime())."\n";
echo ($readIns->getOperation())."\n";
echo ($readIns->getState())."\n";
echo ($readIns->getJobId())."\n";// To get the job_id of bulk read job.
$created_by = $readIns->getCreatedBy();
echo $created_by->getId()."\n";
echo $created_by->getName()."\n";
}
}
$obj =new BulkRead();
$obj->createBulkReadJob();
// Sample request
// API Format: [[[{Last_Name:equal:"PHP Test Lead1"} and [{First_Name:equal:"first_name"} and {Full_Name:equal:"name"}]] or {First_Name:equal:"testsdk"} or {Company:equal:"ZOHO"}] and {Phone:equal:"999-999-9999"}]
// SQL Format: ((((Last_Name:equal:"PHP Test Lead1")and((First_Name:equal:"first_name")and(Full_Name:equal:"name")))or(First_Name:equal:"testsdk")or(Company:equal:"ZOHO"))and(Phone:equal:"999-999-9999"))
Use the below code to know the status of the bulk read job scheduled previously. For more details, refer to Get Job Details page of our API guide.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Get Bulk Read Job Details */
public static function getBulkReadJobDetails()
{
$record= ZCRMRestClient::getInstance()->getBulkReadInstance(null,"87468952942"); // To get the ZCRMBulkRead instance using job_id
$response = $record->getBulkReadJobDetails();
$read = $response->getData();// To get ZCRMBulkRead instance
echo $read->getCreatedTime()."\n";
$created_by = $read->getCreatedBy();
echo $created_by->getId()."\n";
echo $created_by->getName()."\n";
echo $read->getOperation()."\n";
echo $read->getState()."\n";
echo $read->getJobId()."\n";
$query = $read->getQuery();// To get ZCRMBulkQuery instance.
echo $query->getPage()."\n";
print_r($query->getFields());// To get list of Fields.
echo "\n";
echo $query->getModuleAPIName()."\n";
echo $query->getCvId()."\n";// To get module custom view id.
$criteria = $query->getCriteria();
echo $criteria->getAPIName()."\n";
var_dump($criteria->getValue());
echo $criteria->getGroupOperator()."\n";
echo $criteria->getComparator()."\n";
$groups = $criteria->getGroup();
foreach($groups as $group )
{
echo $group->getAPIName()."\n";
var_dump($group->getValue());
echo $group->getGroupOperator()."\n";
echo $group->getComparator()."\n";
print_r($group->getGroup())."\n";
}
echo $query->getCriteriaPattern()."\n";// To get query criteria pattern(eg:(1and(((2and(3and4))or5or6)and7))).
echo $query->getCriteriaCondition();// To get query criteria condition(eg:(($converted:equal:false)and((((Last_Name:equal:"PHP Test Lead1")and((First_Name:equal:"first_name")and(Full_Name:equal:"name")))or(First_Name:equal:"testsdk")or(Company:equal:"ZOHO"))and(Phone:equal:"999-999-9999")))).
echo "\n";
$result = $read->getResult();// To get ZCRMBulkResult instance(It is available only after the completion of the job).
echo $result->getCount()."\n";// To get the actual number of records exported.
echo $result->getDownloadUrl()."\n"; // To get the url which contains the CSV file.
var_dump($result->getMoreRecords())."\n";
echo $result->getPage()."\n";
echo $result->getPerPage()."\n";
}
}
$obj =new BulkRead();
$obj->getBulkReadJobDetails();
Use the below code to download the result of the bulk read job. The response contains a zip file. Extract it to get the CSV or ICS file depending on the "file_type" you specified while creating the bulk read job. For more details, refer to the Download Result page of our API guide.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Download Bulk Read Result */
public static function downloadBulkReadResult()
{
$record= ZCRMRestClient::getInstance()->getBulkReadInstance(null,"743593543"); // To get the ZCRMBulkRead instance using job_id
$response = $record->downloadBulkReadResult();
$fp = fopen("/Users/Desktop/".$response->getFileName(), "w"); // $filePath - absolute path where downloaded file has to be stored.
echo "HTTP Status Code:" . $response->getHttpStatusCode();
echo "File Name:" . $response->getFileName();
$stream = $response->getFileContent();// To get response file stream.
fputs($fp, $stream);
fclose($fp);
}
}
$obj =new BulkRead();
$obj->downloadBulkReadResult();
Use the below code to download the zip file, extract the CSV file, and display the records as instances.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Download the zip and Get list of record instances*/
public static function DowmloadandGetRecords()
{
try {
$recordIns= ZCRMRestClient::getInstance()->getBulkReadInstance("Leads","5352525"); // To get the ZCRMBulkRead instance using job_id
$fileResponse = $recordIns->downloadANDGetRecords("/Users/Desktop");// To download the zip and get list of record instances.
while($fileResponse->hasNext())// To iterate class instance
{
$record = $fileResponse->next();// To get ZCRMRecord instance.
echo $record->getEntityId()."\n";// To get record Id.
echo $record->getCreatedTime()."\n";
echo $record->getFieldValue("Company")."\n";//To get particular field value.
echo $record->getModifiedTime()."\n";
echo $record->getModuleAPIName()."\n";
$owner= $record->getOwner();
if($owner!=null)
{
echo $owner->getId()."\n";
}
$modifiedBy= $record->getModifiedBy();
if($modifiedBy != null)
{
echo $modifiedBy->getId()."\n";
}
$createdBy = $record->getCreatedBy();
if($createdBy != null)
{
echo $createdBy->getId()."\n";
}
foreach($record->getData() as $key=>$value)
{
echo "Key\t:".$key."\tValue:".$value."\n";
}
// $fileResponse->close();// use this method close the file reader
}
} catch (Exception $e) {
print_r($e);
}
}
}
$obj =new BulkRead();
$obj->DowmloadandGetRecords();
Use the below code to download the zip file, extract the ICS file, and display the records as instances.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Get the list of record instances */
/** This method used for ics file type supported module. */
public static function DownloadandGetRecords()
{
try
{
$recordIns= ZCRMRestClient::getInstance()->getBulkReadInstance("Events","535252523");// To get the ZCRMBulkRead instance using job_id
$fileResponse = $recordIns->downloadANDGetRecords("/Users/Desktop");// To download the zip and get list of record instances.
foreach($fileResponse->getData() as $key=>$value)
{
echo "Key\t:".$key."\tValue:";
if($key=="EventsData")
{
while($value->hasNext())
{
$record = $value->next();
foreach($record->getData() as $key1=>$value1)
{
echo "\nKey1\t:".$key1."\tValue1:".$value1."\n";
}
//$value->close();// use this method close the file reader
}
}
else
{
echo $value."\n";
}
}
}
catch (Exception $e)
{
print_r($e);
}
}
}
$obj =new BulkRead();
$obj->DownloadandGetRecords();
Use the below code to fetch all the records from the CSV file mentioned in the file path.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Get the list of record instances */
public static function GetRecordsFromFile()
{
$recordIns= ZCRMBulkRead::getInstance("ModuleAPIName");// To get the ZCRMBulkRead instance of the module
$fileResponse = $recordIns->GetRecords("/Users/Desktop", "zip_file_name");// Absolute path of the downloaded zip and the name of the zip file to fetch record.
while($fileResponse->hasNext())
{
$record = $fileResponse->next();
echo $record->getEntityId()."\n";
echo $record->getCreatedTime()."\n";
echo $record->getFieldValue("Company")."\n";
echo $record->getModifiedTime()."\n";
echo $record->getModuleAPIName()."\n";
$owner= $record->getOwner();
if($owner!=null)
{
echo $owner->getId()."\n";
}
$modifiedBy= $record->getModifiedBy();
if($modifiedBy != null)
{
echo $modifiedBy->getId()."\n";
}
$createdBy = $record->getCreatedBy();
if($createdBy != null)
{
echo $createdBy->getId()."\n";
}
echo $record->getRecordRowNumber()."\n";
foreach($record->getData() as $key => $value)
{
echo "Key\t:".$key."\tValue:".$value."\n";
}
// $fileResponse->close();// use this method close the file reader
}
}
}
$obj =new BulkRead();
$obj->GetRecordsFromFile();
Use the below code to fetch all the records from the ICS file mentioned in the file path.
<?php
use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCallBack;
use zcrmsdk\crm\bulkcrud\ZCRMBulkCriteria;
use zcrmsdk\crm\bulkcrud\ZCRMBulkQuery;
use zcrmsdk\crm\bulkcrud\ZCRMBulkRead;
require_once __DIR__ . '/vendor/autoload.php';
class BulkRead
{
public function __construct()
{
$configuration = array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
}
/** Get the list of record instances */
/** This method used for ics file type supported module. */
public static function GetRecordsFromFile()
{
$recordIns= ZCRMBulkRead::getInstance("ModuleAPIName");// To get the ZCRMBulkRead instance of the module
$fileResponse = $recordIns->GetRecords("/Users/Desktop", "zip_file_name");// Absolute path of the downloaded zip and the name of the zip file to fetch record.
foreach($fileResponse->getData() as $key=>$value)
{
echo "Key\t:".$key."\tValue:";
if($key=="EventsData")
{
while($value->hasNext())
{
$record = $value->next();
foreach($record->getData() as $key1=>$value1)
{
echo "\nKey1\t:".$key1."\tValue1:".$value1."\n";
}
//$value->close();// use this method close the file reader
}
}
else
{
echo $value."\n";
}
}
}
}
$obj =new BulkRead();
$obj->GetRecordsFromFile();