Split Microsoft Word document in PHP using Aspose.Words REST API returns 500 error

https://api.aspose.cloud/v4.0/words/cw.docx/split?format=png

            $chimg = curl_init();
	curl_setopt($chimg, CURLOPT_URL, 'https://api.aspose.cloud/v4.0/words/cw.docx/split?format=png');
	curl_setopt($chimg, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($chimg, CURLOPT_CUSTOMREQUEST, 'PUT');
	curl_setopt($chimg, CURLOPT_VERBOSE, true);  
	 
	
	$headersim = array();
	
	$headersim[] = 'Content-Type: application/json';
	$headersim[] = 'Accept: application/json';
	$headersim[] = 'Content-Length: 0';
	$headersim[] = 'Authorization: Bearer <accesstoken>';
	curl_setopt($chimg, CURLOPT_HTTPHEADER, $headersim);
	
	$resultim = curl_exec($chimg);
	if (curl_errno($chimg)) 
	{
		echo 'Error:' . curl_error($chimg);
	}
	else
	{
		$info = curl_getinfo($chimg);
                    print_r($info);
	}
	curl_close($chimg);

Above code is returning 500 error.

@w3nuts

Thanks for your inquiry. I have tested the scenario with a Word file from Aspose default storage and unable to notice the reported issue. Please note V4 API Version supports JSON Web Token authentication only. Please try the following cURL example, hopefully it will resolve the issue. However, if the issue persists, then please share your sample input document here. We will further investigate the issue and guide you accordingly.

// First get JSON Web Token
// Get App Key and App SID from https://dashboard.aspose.cloud/
curl -v "https://api.aspose.cloud/connect/token" -X POST -d "grant_type=client_credentials&client_id=[MyAPP_SID]&client_secret=[MyAPP_KEY]" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json"

// Split Word Document and Save to ZIP file
curl -X PUT "https://api.aspose.cloud/v4.0/words/TOC_Test.docx/split?format=png&zipOutput=true" -H "accept: application/json" -H "authorization: Bearer [Access_Token]"

Hello Ahmad,

Thanks for your reply.

With this reply i am attaching documents. which we are uploading to aspose.
We are using PHP and CURL to achieve our requirement.

            PHP code to get access token is
            
            $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://api.aspose.cloud/oauth2/token');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials&client_id= MyAPP_SIDclient_secret=MyAPP_KEY");
	curl_setopt($ch, CURLOPT_POST, 1);
	
	$headers = array();
	$headers[] = 'Content-Type: application/x-www-form-urlencoded';
	$headers[] = 'Accept: application/json';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	
	$result = curl_exec($ch);
	if (curl_errno($ch)) 
	{
		echo 'Error:' . curl_error($ch);
	}
	else
	{
		$resultfin = json_decode($result,true);
		$access_token = $resultfin['access_token'];
	}
	curl_close($ch);

            PHP code to split document to image is
                    
                    $ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, 'https://api.aspose.cloud/v4.0/words/cw.docx/split?format=png&zipOutput=true');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
		curl_setopt($ch, CURLOPT_VERBOSE, true);  
		
		$headers = array();
		$headers[] = 'Accept: application/json';
		$headers[] = 'Authorization: Bearer [access_token]';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		
		$result = curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}
		else
		{
			$info = curl_getinfo($ch);
			echo '<pre>';
			print_r($info);
			echo '</pre>';
		}
		curl_close($ch);

We are getting CURL error 411. Please have a look and let me know.

document.zip (280.4 KB)

@w3nuts

As suggested above, you need to use JSON Web Token authentication with V4 API Version instead of Oauth2. Please check again sample authentication cURL code, it will help you to accomplish the task.

// First get JSON Web Token
// Get App Key and App SID from https://dashboard.aspose.cloud/
curl -v "https://api.aspose.cloud/connect/token" 
-X POST 
-d "grant_type=client_credentials&client_id=[MyAPP_SID]&client_secret=[MyAPP_KEY]" 
-H "Content-Type: application/x-www-form-urlencoded" 
-H "Accept: application/json"