|
1 | | -# [Conversion Tools](https://conversiontools.io) API PHP Client |
| 1 | +# [Conversion Tools](https://conversiontools.io) PHP Client |
2 | 2 |
|
3 | | -[Conversion Tools](https://conversiontools.io) is an online service that offers a fast and easy way to convert documents between different formats, like XML, Excel, PDF, Word, Text, CSV and others. |
| 3 | +[Conversion Tools](https://conversiontools.io) is an online service for converting files between formats — XML, Excel, PDF, Word, CSV, JSON, images, audio, video, and more. |
4 | 4 |
|
5 | | -This client allows to integrate the conversion of the files into PHP applications. |
| 5 | +This library integrates file conversion into PHP applications via the [Conversion Tools REST API](https://conversiontools.io/api-documentation). |
6 | 6 |
|
7 | | -To convert the files PHP Client uses the public [Conversion Tools REST API](https://conversiontools.io/api-documentation). |
| 7 | +## Requirements |
8 | 8 |
|
9 | | -## Installation |
| 9 | +- PHP 8.1 or later |
| 10 | +- ext-curl, ext-json (enabled by default in standard PHP installs) |
10 | 11 |
|
11 | | -### Installation using Composer |
| 12 | +## Installation |
12 | 13 |
|
13 | 14 | ```bash |
14 | 15 | composer require conversiontools/conversiontools-php |
15 | 16 | ``` |
16 | 17 |
|
17 | | -or without composer: |
| 18 | +## Quick Start |
18 | 19 |
|
19 | | -```php |
20 | | -require_once('conversiontools-php/src/autoload.php'); |
21 | | -``` |
22 | | - |
23 | | -## Examples |
24 | | - |
25 | | -To use REST API - get API Token from the Profile page at https://conversiontools.io/profile. |
26 | | - |
27 | | -### Converting file |
28 | | - |
29 | | -See example `convert-file.php` in the `./examples/` folder. |
| 20 | +Get your API token from [conversiontools.io/profile](https://conversiontools.io/profile). |
30 | 21 |
|
31 | 22 | ```php |
32 | 23 | <?php |
33 | 24 |
|
34 | 25 | require_once __DIR__ . '/vendor/autoload.php'; |
35 | 26 |
|
36 | | -use \ConversionTools\ConversionClient; |
| 27 | +use ConversionTools\ConversionToolsClient; |
| 28 | + |
| 29 | +$client = new ConversionToolsClient([ |
| 30 | + 'api_token' => 'your-api-token', |
| 31 | +]); |
37 | 32 |
|
38 | | -// put token here from your Profile page at https://conversiontools.io/profile |
39 | | -$token = ''; |
| 33 | +$client->convert('convert.pdf_to_word', 'input.pdf', 'output.docx'); |
| 34 | +``` |
40 | 35 |
|
41 | | -$fileOrUrlInput = 'test.xml'; |
42 | | -$fileOutput = 'test.csv'; |
| 36 | +## Examples |
43 | 37 |
|
44 | | -$options = ['delimiter' => 'tabulation']; |
| 38 | +### Convert a file |
45 | 39 |
|
46 | | -$client = new ConversionClient($token); |
47 | | -try { |
48 | | - $client->convert('convert.xml_to_csv', $fileOrUrlInput, $fileOutput, $options); |
49 | | -} catch (Exception $e) { |
50 | | - print 'Exception: ' . $e->getMessage() . "\n"; |
51 | | -} |
| 40 | +```php |
| 41 | +$client->convert('convert.xml_to_csv', 'data.xml', 'data.csv', [ |
| 42 | + 'delimiter' => 'tabulation', |
| 43 | +]); |
52 | 44 | ``` |
53 | 45 |
|
54 | | -### Converting URL |
| 46 | +### Convert a URL |
55 | 47 |
|
56 | | -See example `convert-url.php` in the `./examples/` folder. |
| 48 | +```php |
| 49 | +$client->convert('convert.website_to_pdf', ['url' => 'https://example.com'], 'result.pdf'); |
| 50 | +``` |
| 51 | + |
| 52 | +### Use a pre-uploaded file |
57 | 53 |
|
58 | 54 | ```php |
59 | | -<?php |
| 55 | +$fileId = $client->files->upload('input.pdf'); |
60 | 56 |
|
61 | | -require_once __DIR__ . '/vendor/autoload.php'; |
| 57 | +$client->convert('convert.pdf_to_word', ['file_id' => $fileId], 'output.docx'); |
| 58 | +``` |
62 | 59 |
|
63 | | -use \ConversionTools\ConversionClient; |
| 60 | +### Manual control (upload → task → wait → download) |
64 | 61 |
|
65 | | -// put token here from your Profile page at https://conversiontools.io/profile |
66 | | -$token = ''; |
| 62 | +```php |
| 63 | +$fileId = $client->files->upload('input.pdf'); |
67 | 64 |
|
68 | | -$fileOrUrlInput = 'https://google.com'; |
69 | | -$fileOutput = 'result.pdf'; |
| 65 | +$task = $client->createTask('convert.pdf_to_word', ['file_id' => $fileId]); |
70 | 66 |
|
71 | | -$options = []; |
| 67 | +$task->wait([ |
| 68 | + 'on_progress' => function (array $status): void { |
| 69 | + echo "{$status['conversionProgress']}% [{$status['status']}]\n"; |
| 70 | + }, |
| 71 | +]); |
72 | 72 |
|
73 | | -$client = new ConversionClient($token); |
74 | | -try { |
75 | | - $client->convert('convert.website_to_pdf', $fileOrUrlInput, $fileOutput, $options); |
76 | | -} catch (Exception $e) { |
77 | | - print 'Exception: ' . $e->getMessage() . "\n"; |
78 | | -} |
| 73 | +$task->downloadTo('output.docx'); |
79 | 74 | ``` |
80 | 75 |
|
81 | | -## API |
| 76 | +### Fire and forget (webhook) |
82 | 77 |
|
83 | | -### Create `ConversionClient` instance with a token. |
84 | 78 | ```php |
85 | | -use \ConversionTools\ConversionClient; |
86 | | - |
87 | | -$client = new ConversionClient('<token>'); |
| 79 | +$taskId = $client->convert( |
| 80 | + conversionType: 'convert.pdf_to_word', |
| 81 | + input: 'input.pdf', |
| 82 | + wait: false, |
| 83 | + callbackUrl: 'https://your-app.com/webhook', |
| 84 | +); |
88 | 85 | ``` |
89 | 86 |
|
90 | | -Where `<token>` is API token from the account's Profile page https://conversiontools.io/profile. |
| 87 | +### Error handling |
91 | 88 |
|
92 | | -### Convert input file and download the result |
93 | 89 | ```php |
94 | | -$client = new ConversionClient($token); |
| 90 | +use ConversionTools\Exceptions\ConversionToolsException; |
| 91 | +use ConversionTools\Exceptions\RateLimitException; |
| 92 | +use ConversionTools\Exceptions\ConversionException; |
| 93 | + |
95 | 94 | try { |
96 | | - $client->convert('<conversion type>', $fileOrUrlInput, $fileOutput, $options); |
97 | | -} catch (Exception $e) { |
98 | | - print 'Exception: ' . $e->getMessage() . "\n"; |
| 95 | + $client->convert('convert.pdf_to_word', 'input.pdf', 'output.docx'); |
| 96 | +} catch (RateLimitException $e) { |
| 97 | + echo "Quota exceeded. Daily remaining: {$e->limits['daily']['remaining']}\n"; |
| 98 | +} catch (ConversionException $e) { |
| 99 | + echo "Conversion failed for task {$e->taskId}: {$e->taskError}\n"; |
| 100 | +} catch (ConversionToolsException $e) { |
| 101 | + echo "Error [{$e->errorCode}]: {$e->getMessage()}\n"; |
99 | 102 | } |
100 | 103 | ``` |
101 | 104 |
|
102 | | -Where |
103 | | -- `<conversion type>` is a specific type of conversion, from [API Documentation](https://conversiontools.io/api-documentation). |
104 | | -- `$fileOrUrlInput` is the filename of the input file, or URL of the file to convert (when applicable) |
105 | | -- `$fileOutput` is the filename of the output file |
106 | | -- `$options` is a PHP array with options for a corresponding converter, for example: |
| 105 | +## API |
| 106 | + |
| 107 | +### `new ConversionToolsClient(array $config)` |
| 108 | + |
| 109 | +| Key | Type | Default | Description | |
| 110 | +|---|---|---|---| |
| 111 | +| `api_token` | string | **required** | API token from your profile | |
| 112 | +| `base_url` | string | `https://api.conversiontools.io/v1` | API base URL | |
| 113 | +| `timeout` | float (ms) | `300000` | Request timeout | |
| 114 | +| `retries` | int | `3` | Retry attempts on transient errors | |
| 115 | +| `retry_delay` | float (ms) | `1000` | Initial retry delay (doubles each attempt) | |
| 116 | +| `polling_interval` | float (ms) | `5000` | How often to poll task status | |
| 117 | +| `max_polling_interval` | float (ms) | `30000` | Max polling interval (with backoff) | |
| 118 | +| `polling_backoff` | float | `1.5` | Polling backoff multiplier | |
| 119 | +| `webhook_url` | string | `null` | Default webhook URL for all tasks | |
| 120 | +| `on_conversion_progress` | callable | `null` | Called on each poll with progress info | |
| 121 | + |
| 122 | +### `convert(string $conversionType, string|array $input, ?string $output, array $options, bool $wait, ?string $callbackUrl): string` |
| 123 | + |
| 124 | +One-call conversion. Returns the output file path (if `$wait=true`) or task ID (if `$wait=false`). |
| 125 | + |
| 126 | +**Input formats:** |
| 127 | +- `'path/to/file.pdf'` — local file path |
| 128 | +- `['url' => 'https://...']` — URL-based conversion |
| 129 | +- `['file_id' => '...']` — pre-uploaded file |
| 130 | + |
| 131 | +### `createTask(string $conversionType, array $options, ?string $callbackUrl): Task` |
| 132 | + |
| 133 | +Create a task without waiting. Returns a `Task` object. |
| 134 | + |
| 135 | +### `getTask(string $taskId): Task` |
| 136 | + |
| 137 | +Retrieve an existing task by ID. |
| 138 | + |
| 139 | +### `getRateLimits(): ?array` |
| 140 | + |
| 141 | +Returns rate limit info from the last API response. |
| 142 | + |
107 | 143 | ```php |
108 | | -$options = ['delimiter' => 'tabulation']; |
| 144 | +$limits = $client->getRateLimits(); |
| 145 | +// ['daily' => ['limit' => 30, 'remaining' => 25], 'monthly' => [...]] |
109 | 146 | ``` |
110 | 147 |
|
111 | | -## Requirements |
112 | | - |
113 | | -PHP version 5.4.0 or later. |
| 148 | +### `Task` |
| 149 | + |
| 150 | +| Method | Description | |
| 151 | +|---|---| |
| 152 | +| `wait(array $options = [])` | Poll until complete. Accepts `polling_interval`, `max_polling_interval`, `timeout`, `on_progress` | |
| 153 | +| `downloadTo(?string $path)` | Download result to file, returns resolved path | |
| 154 | +| `downloadBytes()` | Download result as string | |
| 155 | +| `refresh()` | Re-fetch status from API | |
| 156 | +| `getStatus()` | Returns current status string | |
| 157 | +| `isSuccess()` / `isError()` / `isRunning()` / `isComplete()` | Status helpers | |
| 158 | +| `toArray()` | Serialize task state | |
| 159 | + |
| 160 | +### `$client->files` |
| 161 | + |
| 162 | +| Method | Description | |
| 163 | +|---|---| |
| 164 | +| `upload(string $filePath): string` | Upload file, returns `file_id` | |
| 165 | +| `getInfo(string $fileId): array` | Get file metadata | |
| 166 | +| `downloadBytes(string $fileId): string` | Download as string | |
| 167 | +| `downloadTo(string $fileId, ?string $outputPath): string` | Download to file | |
| 168 | + |
| 169 | +### `$client->tasks` |
| 170 | + |
| 171 | +| Method | Description | |
| 172 | +|---|---| |
| 173 | +| `create(array $request): array` | Create task (low-level) | |
| 174 | +| `getStatus(string $taskId): array` | Get task status | |
| 175 | +| `list(?string $status): array` | List tasks, optionally filtered by status | |
| 176 | + |
| 177 | +### Exceptions |
| 178 | + |
| 179 | +All exceptions extend `ConversionToolsException` and expose `$errorCode` and `$httpStatus`. |
| 180 | + |
| 181 | +| Exception | Trigger | |
| 182 | +|---|---| |
| 183 | +| `AuthenticationException` | Invalid or missing API token | |
| 184 | +| `ValidationException` | Bad request parameters | |
| 185 | +| `RateLimitException` | Quota exceeded — has `$limits` property | |
| 186 | +| `FileNotFoundException` | File ID not found | |
| 187 | +| `TaskNotFoundException` | Task ID not found | |
| 188 | +| `ConversionException` | Task failed — has `$taskId` and `$taskError` | |
| 189 | +| `TimeoutException` | Request or polling timed out | |
| 190 | +| `NetworkException` | Connection error | |
114 | 191 |
|
115 | 192 | ## Documentation |
116 | 193 |
|
117 | | -List of available Conversion Types and corresponding conversion options can be found on the [Conversion Tools API Documentation](https://conversiontools.io/api-documentation) page. |
| 194 | +Full list of conversion types and options: [conversiontools.io/api-documentation](https://conversiontools.io/api-documentation) |
118 | 195 |
|
119 | 196 | ## License |
120 | 197 |
|
121 | | -Licensed under [MIT](./LICENSE). |
122 | | - |
123 | | -Copyright (c) 2021 [Conversion Tools](https://conversiontools.io) |
| 198 | +Licensed under [MIT](./LICENSE). Copyright (c) [Conversion Tools](https://conversiontools.io) |
0 commit comments