Zoho | How to download attachments for a contact from Zoho using Cakephp

Author - Deepika
5.09.2017
|
0 Comments
|

To download attachments from Zoho CRM from a module, like Contacts, fetch detail of all the attachments related to that module, then download each attachment by its attachment id using download file API in Zoho and finally convert blob to the image file.

For Examle: Download attachment from contacts for a particular contact id. This contains following steps:
1. Fetch the detail of attachments related to Contact (for passed contact id).
2. Using each attachment id to get the data of the attachment in blob form.
3. Convert the blob data to file:

use Cake\Http\Client;
function test($contactId='xxxxx', $token='xxxxxx')
{
try {
 $http = new Client
 ( [
 'headers' => [
 'Content-Type' => 'application/json ]
  ] );
//Fetch the detail of attachments related to Contact.
$response = $http->post (
https://crm.zoho.com/crm/private/json/Attachments/getRelatedRecords', [
	'authtoken' => $token,
	'version' => '4',
	'scope' => 'crmapi',
	'id' => $contactId,
	"parentModule" => "Contacts",
	"newFormat" => "1"
	]);	
	$result = $response->json;
	//Search Attachement Result received from Zoho
	$preArray = $result ['response'] ['result'] ['Attachments'] ['row'];
	$first_key1 = key ( $preArray );
	if ($first_key1 == "0") {
	foreach ( $preArray as $array ) 
        {
	$attachmentIds [] = $array ['FL'] [0] ['content'];
        $resultdata ['name'] [] = $array ['FL'] [1] ['content'];
	}
	} 
else {
	$attachmentIds [] = $preArray ['FL'] [0] ['content'];
	$resultdata ['name'] [] = $preArray ['FL'] [1] ['content'];
	}
	$i=0;
	foreach (
        $attachmentIds as $id ) {
	$http = new Client ( [
        'headers' => [
	'Content-Type' => 'application/json'] );
//Using each attachment id get the data of the attachment in blob form
	$response = $http->get ( 'https://crm.zoho.com/crm/private/json/Contacts/downloadFile', [
	'authtoken' => $token,
	'version' => '4',
	'scope' => 'crmapi',
	'id' => $id
	] );
//Convert the blob data to file
	$path = $contactId. $resultdata['name'] [$i];
	file_put_contents ( $path, $response->body);
	$i++;
	}
	} catch ( Exception $e ) 
        {
	echo 'Caught exception: ', $e->getMessage (), "\n";
        }
	}

Where $token is the Zoho CRM token and $contactid is the CRM id of the contact from which you want to download the attachments.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send your message