Skip to main content

PHP SDK Samples - Files Operations

Upload File
Get File
              
              
<?php
namespace com\zoho\crm\sample\file;
use com\zoho\crm\api\ParameterMap;
use com\zoho\crm\api\file\APIException;
use com\zoho\crm\api\file\ActionWrapper;
use com\zoho\crm\api\file\BodyWrapper;
use com\zoho\crm\api\file\FileOperations;
use com\zoho\crm\api\file\SuccessResponse;
use com\zoho\crm\api\file\GetFileParam;
use com\zoho\crm\api\util\StreamWrapper;
class File
{
	/**
	 *  Get File
	 * @param id - The ID of the uploaded File.
	 * @param destinationFolder - The absolute path of the destination folder to store the File
	 * @throws Exception
	 */
	public static function getFile(string $id, string $destinationFolder)
	{
		//example
		//id = "123XXXXXX000";
		//destinationFolder = "/Users/user_name/Desktop"
		//Get instance of FileOperations Class
		$fileOperations = new FileOperations();
		//Get instance of ParameterMap Class
		$paramInstance = new ParameterMap();
		$paramInstance->add(GetFileParam::id(), $id);
		//Call getFile method that takes paramInstance as parameters
		$response = $fileOperations->getFile($paramInstance);
		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 object from response
                $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");
            }
		}
	}
}