PHP SDK Samples - Attachment Operations

Get Attachments
              
              
namespace samples\attachments;

use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\attachments\AttachmentsOperations;
use com\zoho\crm\api\attachments\ResponseWrapper;
use com\zoho\crm\api\attachments\APIException;
use com\zoho\crm\api\ParameterMap;
use com\zoho\crm\api\attachments\GetAttachmentsParam;
require_once "vendor/autoload.php";

class GetAttachments
{
    public static function initialize()
    {
        $user = new UserSignature('myname@mydomain.com');
        $environment = USDataCenter::PRODUCTION();
        $token = (new OAuthBuilder())
        ->clientId("1000.xxxx")
        ->clientSecret("xxxxxx")
        ->refreshToken("1000.xxxxx.xxxxx")
        ->build();
        (new InitializeBuilder())
            ->user($user)
            ->environment($environment)
            ->token($token)
            ->initialize();
    }

    public static function getAttachments(string $moduleAPIName, string $recordId)
    {
        $attachmentOperations = new AttachmentsOperations($moduleAPIName, $recordId);
        $paramInstance = new ParameterMap();
        // $paramInstance->add(GetAttachmentsParam::fields(), "id,Modified_Time");
        $paramInstance->add(GetAttachmentsParam::page(), 1);
        $paramInstance->add(GetAttachmentsParam::perPage(), 100);
        $response = $attachmentOperations->getAttachments($paramInstance);
        if($response != null)
		{
            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;
            }
            $responseHandler = $response->getObject();
            if($responseHandler instanceof ResponseWrapper)
            {
                $responseWrapper = $responseHandler;
                $attachments = $responseWrapper->getData();
                foreach ($attachments as $attachment)
                {
                    $owner = $attachment->getOwner();
                    if($owner != null)
                    {
                        echo("Attachment Owner User-Name: " . $owner->getName() . "\n");
                        echo("Attachment Owner User-ID: " . $owner->getId() . "\n");
                        echo("Attachment Owner User-Email: " . $owner->getEmail() . "\n");
                    }
                    echo("Attachment Modified Time: "); print_r($attachment->getModifiedTime()); echo("\n");
                    echo("Attachment File Name: " . $attachment->getFileName() . "\n");
                    echo("Attachment Created Time: " ); print_r($attachment->getCreatedTime()); echo("\n");
                    echo("Attachment File Size: " . $attachment->getSize() . "\n");
                    $parentId = $attachment->getParentId();
                    if($parentId != null)
                    {
                        echo("Attachment parent record Name: " . $parentId->getKeyValue("name") . "\n");
                        echo("Attachment parent record ID: " . $parentId->getId() . "\n");
                    }
                    echo("Attachment is Editable: " . $attachment->getEditable() . "\n");
                    echo("Attachment SharingPermission: " . $attachment->getSharingPermission() . "\n");
                    echo("Attachment File ID: " . $attachment->getFileId() . "\n");
                    echo("Attachment File Type: " . $attachment->getType() . "\n");
                    echo("Attachment seModule: " . $attachment->getSeModule() . "\n");
                    $modifiedBy = $attachment->getModifiedBy();
                    if($modifiedBy != null)
                    {
                        echo("Attachment Modified By User-Name: " . $modifiedBy->getName() . "\n");
                        echo("Attachment Modified By User-ID: " . $modifiedBy->getId() . "\n");
                        echo("Attachment Modified By User-Email: " . $modifiedBy->getEmail() . "\n");
                    }
                    echo("Attachment Type: " . $attachment->getAttachmentType() . "\n");
                    echo("Attachment State: " . $attachment->getState() . "\n");
                    echo("Attachment ID: " . $attachment->getId() . "\n");
                    $createdBy = $attachment->getCreatedBy();
                    if($createdBy != null)
                    {
                        echo("Attachment Created By User-Name: " . $createdBy->getName() . "\n");
                        echo("Attachment Created By User-ID: " . $createdBy->getId() . "\n");
                        echo("Attachment Created By User-Email: " . $createdBy->getEmail() . "\n");
                    }
                    echo("Attachment LinkUrl: " . $attachment->getLinkUrl() . "\n");
                }
                $info = $responseWrapper->getInfo();
                echo("Attachment Info PerPage : " . $info->getPerPage() . "\n");
                echo("Attachment Info Count : " . $info->getCount() . "\n");
                echo("Attachment Info Page : " . $info->getPage(). "\n");
                echo("Attachment Info MoreRecords : "); print_r($info->getMoreRecords()); echo("\n");
            }
            else if($responseHandler instanceof APIException)
            {
                $exception = $responseHandler;
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                    foreach ($exception->getDetails() as $keyName => $keyValue)
                    {
                        echo($keyName . ": " . $keyValue . "\n");
                    }
                }
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}

GetAttachments::initialize();
$moduleAPIName = "Leads";
$recordId = "347706111829018";
GetAttachments::getAttachments($moduleAPIName, $recordId);

 
Upload Attachments
              
              
namespace samples\attachments;

use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\attachments\AttachmentsOperations;
use com\zoho\crm\api\attachments\APIException;
use com\zoho\crm\api\attachments\FileBodyWrapper;
use com\zoho\crm\api\util\StreamWrapper;
use com\zoho\crm\api\attachments\SuccessResponse;
use com\zoho\crm\api\attachments\ActionWrapper;
require_once "vendor/autoload.php";

class UploadAttachments
{
    public static function initialize()
    {
        $user = new UserSignature('myname@mydomain.com');
        $environment = USDataCenter::PRODUCTION();
        $token = (new OAuthBuilder())
        ->clientId("1000.xxxx")
        ->clientSecret("xxxxxx")
        ->refreshToken("1000.xxxxx.xxxxx")
        ->build();
        (new InitializeBuilder())
            ->user($user)
            ->environment($environment)
            ->token($token)
            ->initialize();
    }

    public static function uploadAttachments(string $moduleAPIName, string $recordId, string $absoluteFilePath)
    {
        $attachmentOperations = new AttachmentsOperations($moduleAPIName, $recordId);
        $fileBodyWrapper = new FileBodyWrapper();
        $streamWrapper = new StreamWrapper(null, null, $absoluteFilePath);
		$fileBodyWrapper->setFile($streamWrapper);
        $response = $attachmentOperations->uploadAttachment($fileBodyWrapper);
        if($response != null)
		{
            echo("Status code " . $response->getStatusCode() . "\n");
            $actionHandler = $response->getObject();
            if($actionHandler instanceof ActionWrapper)
            {
                $actionWrapper = $actionHandler;
                $actionResponses = $actionWrapper->getData();
                foreach ($actionResponses as $actionResponse)
                {
                    if($actionResponse instanceof SuccessResponse)
                    {
                        $successResponse = $actionResponse;
                        echo("Status : " . $successResponse->getStatus()->getValue() . "\n");
                        echo("Code : " . $successResponse->getCode()->getValue() . "\n");
                        echo("Details : \n" );
                        if($successResponse->getDetails() != null)
                        {
                            foreach ($successResponse->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . " : "); print_r($keyValue); echo("\n");
                            }
                        }
                        echo("Message: " . $successResponse->getMessage()->getValue() . "\n");
                    }
                    else if($actionResponse instanceof APIException)
                    {
                        $exception = $actionResponse;
                        echo("Status: " . $exception->getStatus()->getValue() . "\n");
                        echo("Code: " . $exception->getCode()->getValue() . "\n");
                        echo("Details: \n" );
                        if($exception->getDetails() != null)
                        {
                            foreach ($exception->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . ": " . $keyValue . "\n");
                            }
                        }
                        echo("Message: " . $exception->getMessage()->getValue() . "\n");
                    }
                }
            }
            else if($actionHandler instanceof APIException)
            {
                $exception = $actionHandler;
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                    foreach ($exception->getDetails() as $keyName => $keyValue)
                    {
                        echo($keyName . ": " . $keyValue . "\n");
                    }
                }
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}

UploadAttachments::initialize();
$moduleAPIName = "Leads";
$recordId = "347706111829018";
$absoluteFilePath = "/Documents/download.png";
UploadAttachments::uploadAttachments($moduleAPIName, $recordId, $absoluteFilePath);

 
Delete Attachments
              
              
namespace samples\attachments;

use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\attachments\AttachmentsOperations;
use com\zoho\crm\api\attachments\APIException;
use com\zoho\crm\api\attachments\SuccessResponse;
use com\zoho\crm\api\attachments\ActionWrapper;
use com\zoho\crm\api\ParameterMap;
use com\zoho\crm\api\attachments\DeleteAttachmentsParam;
require_once "vendor/autoload.php";

class DeleteAttachments
{
    public static function initialize()
    {
        $user = new UserSignature('myname@mydomain.com');
        $environment = USDataCenter::PRODUCTION();
        $token = (new OAuthBuilder())
        ->clientId("1000.xxxx")
        ->clientSecret("xxxxxx")
        ->refreshToken("1000.xxxxx.xxxxx")
        ->build();
        (new InitializeBuilder())
            ->user($user)
            ->environment($environment)
            ->token($token)
            ->initialize();
    }

	public static function deleteAttachments(string $moduleAPIName, string $recordId, array $attachmentIds)
	{
		$attachmentOperations = new AttachmentsOperations($moduleAPIName, $recordId);
		$paramInstance = new ParameterMap();
		foreach($attachmentIds as $attachmentId)
		{
			$paramInstance->add(DeleteAttachmentsParam::ids(), $attachmentId);
		}
        $response = $attachmentOperations->deleteAttachments($paramInstance);
        if($response != null)
        {
            echo("Status code" . $response->getStatusCode() . "\n");
            $actionHandler = $response->getObject();
            if($actionHandler instanceof ActionWrapper)
            {
                $actionWrapper = $actionHandler;
                $actionResponses = $actionWrapper->getData();
                foreach ($actionResponses as $actionResponse)
                {
                    if($actionResponse instanceof SuccessResponse)
                    {
                        $successResponse = $actionResponse;
                        echo("Status: " . $successResponse->getStatus()->getValue() . "\n");
                        echo("Code: " . $successResponse->getCode()->getValue() . "\n");
                        echo("Details: " );
                        if($successResponse->getDetails() != null)
                        {
                            foreach ($successResponse->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . ": " . $keyValue . "\n");
                            }
                        }
                        echo("Message: " . $successResponse->getMessage()->getValue() . "\n");
                    }
                    else if($actionResponse instanceof APIException)
                    {
                        $exception = $actionResponse;
                        echo("Status: " . $exception->getStatus()->getValue() . "\n");
                        echo("Code: " . $exception->getCode()->getValue() . "\n");
                        echo("Details: " );
                        if($exception->getDetails() != null)
                        {
                            foreach ($exception->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . ": " . $keyValue . "\n");
                            }
                        }
                        echo("Message: " . $exception->getMessage()->getValue() . "\n");
                    }
                }
            }
            else if($actionHandler instanceof APIException)
            {
                $exception = $actionHandler;
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                    foreach ($exception->getDetails() as $keyName => $keyValue)
                    {
                        echo($keyName . ": " . $keyValue . "\n");
                    }
                }
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}

DeleteAttachments::initialize();
$moduleAPIName = "Leads";
$recordId = "347706111829018";
$attachmentIds = array("347706118189001", "34770619773001");
DeleteAttachments::deleteAttachments($moduleAPIName, $recordId, $attachmentIds);
 
Download an Attachment
              
              
namespace samples\attachments;

use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\attachments\AttachmentsOperations;
use com\zoho\crm\api\attachments\APIException;
use com\zoho\crm\api\attachments\FileBodyWrapper;
require_once "vendor/autoload.php";

class DownloadAttachment
{
    public static function initialize()
    {
        $user = new UserSignature('myname@mydomain.com');
        $environment = USDataCenter::PRODUCTION();
        $token = (new OAuthBuilder())
        ->clientId("1000.xxxx")
        ->clientSecret("xxxxxx")
        ->refreshToken("1000.xxxxx.xxxxx")
        ->build();
        (new InitializeBuilder())
            ->user($user)
            ->environment($environment)
            ->token($token)
            ->initialize();
    }

    public static function downloadAttachment(string $moduleAPIName, string $recordId, string $attachmentId, string $destinationFolder)
    {
        $attachmentOperations = new AttachmentsOperations($moduleAPIName, $recordId);
        $response = $attachmentOperations->downloadAttachment($attachmentId);
        if($response != null)
		{
            echo("Status code " . $response->getStatusCode() . "\n");
            if($response->getStatusCode() == 204)
            {
                echo("No Content\n");
                return;
            }
            $responseHandler = $response->getObject();
            if($responseHandler instanceof FileBodyWrapper)
            {
                    $fileBodyWrapper = $responseHandler;
                    $streamWrapper = $fileBodyWrapper->getFile();
                    $fp = fopen($destinationFolder."/".$streamWrapper->getName(), "w");
                    $stream = $streamWrapper->getStream();
                    fputs($fp, $stream);
                    fclose($fp);
            }
            else if($responseHandler instanceof APIException)
            {
                $exception = $responseHandler;
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                    foreach ($exception->getDetails() as $keyName => $keyValue)
                    {
                        echo($keyName . ": " . $keyValue . "\n");
                    }
                }
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}

DownloadAttachment::initialize();
$moduleAPIName = "Leads";
$recordId = "347706111829018";
$attachmentId = "347706111902001";
$destinationFolder = "/Documents";
DownloadAttachment::downloadAttachment($moduleAPIName, $recordId, $attachmentId, $destinationFolder);
 
Delete an Attachment
              
              
namespace samples\attachments;

use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\attachments\AttachmentsOperations;
use com\zoho\crm\api\attachments\APIException;
use com\zoho\crm\api\attachments\SuccessResponse;
use com\zoho\crm\api\attachments\ActionWrapper;
require_once "vendor/autoload.php";

class DeleteAttachment
{
    public static function initialize()
    {
        $user = new UserSignature('myname@mydomain.com');
        $environment = USDataCenter::PRODUCTION();
        $token = (new OAuthBuilder())
        ->clientId("1000.xxxx")
        ->clientSecret("xxxxxx")
        ->refreshToken("1000.xxxxx.xxxxx")
        ->build();
        (new InitializeBuilder())
            ->user($user)
            ->environment($environment)
            ->token($token)
            ->initialize();
    }

    public static function deleteAttachment(string $moduleAPIName, string $recordId, string $attachmentId)
	{
		$attachmentOperations = new AttachmentsOperations($moduleAPIName, $recordId);
        $response = $attachmentOperations->deleteAttachment($attachmentId);
        if($response != null)
        {
            echo("Status code" . $response->getStatusCode() . "\n");
            $actionHandler = $response->getObject();
            if($actionHandler instanceof ActionWrapper)
            {
                $actionWrapper = $actionHandler;
                $actionResponses = $actionWrapper->getData();
                foreach ($actionResponses as $actionResponse)
                {
                    if($actionResponse instanceof SuccessResponse)
                    {
                        $successResponse = $actionResponse;
                        echo("Status: " . $successResponse->getStatus()->getValue() . "\n");
                        echo("Code: " . $successResponse->getCode()->getValue() . "\n");
                        if($successResponse->getDetails() != null)
                        {
                            echo("Details: " );
                            foreach ($successResponse->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . ": " . $keyValue . "\n");
                            }
                        }
                        echo("Message: " . $successResponse->getMessage()->getValue() . "\n");
                    }
                    else if($actionResponse instanceof APIException)
                    {
                        $exception = $actionResponse;
                        echo("Status: " . $exception->getStatus()->getValue() . "\n");
                        echo("Code: " . $exception->getCode()->getValue() . "\n");
                        if($exception->getDetails() != null)
                        {
                            echo("Details: " );
                            foreach ($exception->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . ": " . $keyValue . "\n");
                            }
                        }
                        echo("Message: " . $exception->getMessage()->getValue() . "\n");
                    }
                }
            }
            else if($actionHandler instanceof APIException)
            {
                $exception = $actionHandler;
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                    foreach ($exception->getDetails() as $keyName => $keyValue)
                    {
                        echo($keyName . ": " . $keyValue . "\n");
                    }
                }
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}

DeleteAttachment::initialize();
$moduleAPIName = "Leads";
$recordId = "347706111829018";
$attachmentId = "347706112329001";
DeleteAttachment::deleteAttachment($moduleAPIName, $recordId, $attachmentId);
 
              
              
namespace samples\attachments;

use com\zoho\api\authenticator\OAuthBuilder;
use com\zoho\crm\api\dc\USDataCenter;
use com\zoho\crm\api\InitializeBuilder;
use com\zoho\crm\api\UserSignature;
use com\zoho\crm\api\attachments\AttachmentsOperations;
use com\zoho\crm\api\attachments\APIException;
use com\zoho\crm\api\attachments\SuccessResponse;
use com\zoho\crm\api\attachments\ActionWrapper;
use com\zoho\crm\api\ParameterMap;
use com\zoho\crm\api\attachments\UploadLinkAttachmentParam;
require_once "vendor/autoload.php";

class UploadLinkAttachments
{
    public static function initialize()
    {
        $user = new UserSignature('myname@mydomain.com');
        $environment = USDataCenter::PRODUCTION();
        $token = (new OAuthBuilder())
        ->clientId("1000.xxxx")
        ->clientSecret("xxxxxx")
        ->refreshToken("1000.xxxxx.xxxxx")
        ->build();
        (new InitializeBuilder())
            ->user($user)
            ->environment($environment)
            ->token($token)
            ->initialize();
    }

    public static function uploadLinkAttachments(string $moduleAPIName, string $recordId, string $attachmentURL)
    {
        $attachmentOperations = new AttachmentsOperations($moduleAPIName, $recordId);
        $paramInstance = new ParameterMap();
		$paramInstance->add(UploadLinkAttachmentParam::attachmentUrl(), $attachmentURL);
        $response = $attachmentOperations->uploadLinkAttachment($paramInstance);
        if($response != null)
        {
            echo("Status code" . $response->getStatusCode() . "\n");
            $actionHandler = $response->getObject();
            if($actionHandler instanceof ActionWrapper)
            {
                $actionWrapper = $actionHandler;
                $actionResponses = $actionWrapper->getData();
                foreach ($actionResponses as $actionResponse)
                {
                    if($actionResponse instanceof SuccessResponse)
                    {
                        $successResponse = $actionResponse;
                        echo("Status: " . $successResponse->getStatus()->getValue() . "\n");
                        echo("Code: " . $successResponse->getCode()->getValue() . "\n");
                        echo("Details : \n" );
                        if($successResponse->getDetails() != null)
                        {
                            foreach ($successResponse->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . " : "); print_r($keyValue); echo("\n");
                            }
                        }
                        echo("Message: " . $successResponse->getMessage()->getValue() . "\n");
                    }
                    else if($actionResponse instanceof APIException)
                    {
                        $exception = $actionResponse;
                        echo("Status: " . $exception->getStatus()->getValue() . "\n");
                        echo("Code: " . $exception->getCode()->getValue() . "\n");
                        echo("Details: " );
                        if($exception->getDetails() != null)
                        {
                            foreach ($exception->getDetails() as $keyName => $keyValue)
                            {
                                echo($keyName . ": " . $keyValue . "\n");
                            }
                        }
                        echo("Message: " . $exception->getMessage()->getValue() . "\n");
                    }
                }
            }
            else if($actionHandler instanceof APIException)
            {
                $exception = $actionHandler;
                echo("Status: " . $exception->getStatus()->getValue() . "\n");
                echo("Code: " . $exception->getCode()->getValue() . "\n");
                if($exception->getDetails() != null)
                {
                    echo("Details: \n");
                    foreach ($exception->getDetails() as $keyName => $keyValue)
                    {
                        echo($keyName . ": " . $keyValue . "\n");
                    }
                }
                echo("Message: " . $exception->getMessage()->getValue() . "\n");
            }
        }
    }
}


UploadLinkAttachments::initialize();
$moduleAPIName = "Leads";
$recordId = "347706111829018";
$attachmentURL = "https://5.imimg.com/data5/KJ/UP/MY-8655440/zoho-crm-500x500.png";
UploadLinkAttachments::uploadLinkAttachments($moduleAPIName, $recordId, $attachmentURL);