Skip to main content

PHP SDK Samples - Files Operations

Upload 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
{
 /**
	 *  Upload File
	 * This method is used to upload a file and print the response.
	 * @throws Exception
	 */
	public static function uploadFile()
	{
		//Get instance of RecordOperations Class
		$fileOperations = new FileOperations();
		//Get instance of FileBodyWrapper Class that will contain the request body
		$bodyWrapper = new BodyWrapper();
        //Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
        $streamWrapper = new StreamWrapper(null, null, "/Users/abc-XXX/Desktop/py.html");
        //Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
        $streamWrapper1 = new StreamWrapper(null, null, "/Users/abc-XXX/Desktop/download.png");
        //Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
        $streamWrapper2 = new StreamWrapper(null, null, "/Users/abc-XXXX/Desktop/samplecode.txt");
        //Set file to the FileBodyWrapper instance
        $bodyWrapper->setFile([$streamWrapper, $streamWrapper1, $streamWrapper2 ]);
        //Get instance of ParameterMap Class
        $paramInstance = new ParameterMap();
		//Call uploadFile method that takes BodyWrapper instance as parameter.
		$response = $fileOperations->uploadFile($bodyWrapper, $paramInstance);
		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 action responses
                $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: " );
                        if($successResponse->getDetails() != null)
                        {
                            //Get the details map
                            foreach ($successResponse->getDetails() as $keyName => $keyValue) 
                            {
                                //Get each value in the map
                                echo($keyName . ": " . $keyValue . "\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: " );
                        if($exception->getDetails() != null)
                        {
                            //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");
                    }
                }
            }
            //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: " );
                if($exception->getDetails() != null)
                {
                    //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");
            }
		}
	}
}
 
Get File