Installation Add the SDK via Composer:
composer require aspose/cloud-sdk-php:~1.1
Requires PHP 7.2+ and guzzlehttp/guzzle (automatically installed).
Authentication Initialize the SDK with your Aspose Cloud credentials:
use Aspose\Cloud\Sdk\Common\ApiClient;
$apiClient = new ApiClient();
$apiClient->setAppSID('YOUR_APP_SID');
$apiClient->setAppKey('YOUR_APP_KEY');
First Use Case: Convert a PDF to DOCX
use Aspose\Cloud\Sdk\Pdf\PdfApi;
$pdfApi = new PdfApi($apiClient);
$response = $pdfApi->convertDocument(
'input.pdf', // File in storage
'docx', // Output format
'output.docx'
);
src/Aspose/Cloud/Sdk for module-specific examples.examples folder for ready-to-use snippets.File Operations (Storage Module)
use Aspose\Cloud\Sdk\Storage\StorageApi;
$storageApi = new StorageApi($apiClient);
$storageApi->uploadFile('local.pdf', 'cloud.pdf'); // Upload
$storageApi->downloadFile('cloud.pdf', 'local_copy.pdf'); // Download
$files = $storageApi->listFiles('folder/');
Document Conversion (Pdf/Words/Slides/Cells)
$pdfApi->convertDocument('input.pdf', 'docx', 'output.docx');
$pdfApi->convertDocument('input.pdf', 'html', 'output.html');
$pdfApi->extractText('file.pdf');
use Aspose\Cloud\Sdk\Words\WordsApi;
$wordsApi = new WordsApi($apiClient);
$wordsApi->saveAsHtml('doc.docx', 'output.html');
use Aspose\Cloud\Sdk\Cells\CellsApi;
$cellsApi = new CellsApi($apiClient);
$cellsApi->convertXlsxToCsv('sheet.xlsx', 'output.csv');
Authentication & Rate Limiting
ApiClient to manage credentials and retries:
$apiClient->setAppSID('...')->setAppKey('...');
$apiClient->setTimeout(30); // Adjust timeout as needed
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(ApiClient::class, function ($app) {
$apiClient = new ApiClient();
$apiClient->setAppSID(config('aspose.app_sid'));
$apiClient->setAppKey(config('aspose.app_key'));
return $apiClient;
});
}
use Aspose\Cloud\Sdk\Pdf\PdfApi;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class ConvertPdfJob implements ShouldQueue {
use Queueable;
public function handle(PdfApi $pdfApi) {
$pdfApi->convertDocument('large.pdf', 'docx', 'output.docx');
}
}
try {
$response = $pdfApi->convertDocument('file.pdf', 'docx', 'output.docx');
} catch (\Exception $e) {
Log::error('Aspose API Error: ' . $e->getMessage());
throw new \RuntimeException('Conversion failed', 0, $e);
}
Authentication Issues
401 Unauthorized errors if App SID/Key are incorrect or expired.ApiClient and ensure they have the correct permissions in the Aspose Cloud Dashboard.$apiClient->setAppSID(env('ASPOSE_APP_SID'));
File Size Limits
413 Payload Too Large.Rate Limiting
$apiClient->setRetryPolicy(new \GuzzleHttp\Retry\RetryPolicy(3, 100)); // Retry 3 times with 100ms delay
Storage Paths
uploadFile/downloadFile cause 404 Not Found./) and ensure the path exists in Aspose Storage.Module-Specific Quirks
extractText) return raw text; sanitize before rendering..dotx files; ensure correct template paths.$apiClient->setDebug(true); // Logs requests/responses to storage/logs/
$response = $pdfApi->convertDocument(...);
file_put_contents('debug.json', json_encode($response->getData()));
400 Bad Request: Invalid input (e.g., unsupported format).404 Not Found: File/path does not exist.500 Internal Server Error: Contact Aspose support with request details.Custom Headers
Add headers to API requests via ApiClient:
$apiClient->setDefaultHeader('Custom-Header', 'value');
Proxy Support Configure proxy settings:
$apiClient->setProxy('http://proxy.example.com', 8080, 'user', 'pass');
Event Hooks Extend the SDK by overriding methods in a custom class:
class CustomPdfApi extends PdfApi {
public function convertDocument($name, $format, $outPath) {
// Pre-processing logic
parent::convertDocument($name, $format, $outPath);
// Post-processing logic
}
}
Webhooks for Async Operations Use Aspose’s Webhook API to monitor long-running tasks:
$apiClient->setWebhookUrl('https://your-app.com/aspose/webhook');
parallel helper for batch conversions:
\Bus::parallel([
new ConvertPdfJob('file1.pdf'),
new ConvertPdfJob('file2.pdf'),
]);
How can I help you explore Laravel packages today?