|
1 | | -# compdfkit-api-php |
2 | | -ComPDFKit API PHP SDK |
| 1 | +## ComPDFKit API in PHP |
| 2 | + |
| 3 | +[ComPDFKit](https://api.compdf.com/api/docs/introduction) API provides a variety of PHP API tools that allow you to create an efficient document processing workflow in a single API call. Try our various APIs for free — no credit card required. |
| 4 | + |
| 5 | +In this guide, we’ll go through how you can use PHP to make HTTP requests with ComPDFKit API. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +Programming Environment: PHP Version 7.0 and higher. |
| 12 | + |
| 13 | +Dependencies: Composer. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +You can install the library via Composer. Run the following command. |
| 20 | +``` shell script |
| 21 | +composer require compdfkit/compdfkit-api-php |
| 22 | +``` |
| 23 | +Alternatively, you can add "compdfkit/compdfkit-api-php": "^1.2.4" to your ***"composer.json"*** file and then run it. |
| 24 | +``` shell script |
| 25 | +composer update |
| 26 | +``` |
| 27 | + |
| 28 | +If you are not using a PHP framework with autoload feature, you need to use the code below to autoload. |
| 29 | +```php |
| 30 | +require_once('vendor/autoload.php'); |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +## Create API Client |
| 36 | + |
| 37 | +You can use your **publicKey** and **secretKey** to complete the authentication. You need to [sign in](https://api.compdf.com/login) your ComPDFKit API account to get your **publicKey** and **secretKey** at the [dashboard](https://api-dashboard.compdf.com/api/keys). If you are new to ComPDFKit, click here to [sign up](https://api.compdf.com/signup) for a free trial. |
| 38 | + |
| 39 | +- Project public Key: You can find the public key in [Management Panel](https://api-dashboard.compdf.com/api/keys). |
| 40 | + |
| 41 | +- Project secret Key: You can find the secret Key in [Management Panel](https://api-dashboard.compdf.com/api/keys). |
| 42 | + |
| 43 | +```php |
| 44 | +$client = new CPDFClient('public_key', 'secret_key'); |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +## Create Task |
| 50 | + |
| 51 | +A task ID is automatically generated for you based on the type of PDF tool you choose. You can provide the callback notification URL. After the task processing is completed, we will notify you of the task result through the callback interface. You can perform other operations according to the request result, such as checking the status of the task, uploading files, starting the task, or downloading the result file. |
| 52 | + |
| 53 | +```php |
| 54 | +// Create a client |
| 55 | +$client = new CPDFClient('public_key', 'secret_key'); |
| 56 | + |
| 57 | +// Create a task |
| 58 | +// Create an example of a PDF TO WORD task |
| 59 | +$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD); |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Upload Files |
| 65 | + |
| 66 | +Upload the original file and bind the file to the task ID. The field parameter is used to pass the JSON string to set the processing parameters for the file. Each file will generate automatically a unique filekey. Please note that a maximum of five files can be uploaded for a task ID and no files can be uploaded for that task after it has started. |
| 67 | + |
| 68 | +```php |
| 69 | +// Create a client |
| 70 | +$client = new CPDFClient('public_key', 'secret_key'); |
| 71 | + |
| 72 | +// Create a task |
| 73 | +// Create an example of a PDF TO WORD task |
| 74 | +$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD); |
| 75 | + |
| 76 | +// Upload files |
| 77 | +$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']); |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +## Execute the task |
| 83 | + |
| 84 | +After the file upload is completed, call this interface with the task ID to process the files. |
| 85 | + |
| 86 | +```php |
| 87 | +// Create a client |
| 88 | +$client = new CPDFClient('public_key', 'secret_key'); |
| 89 | + |
| 90 | +// Create a task |
| 91 | +// Create an example of a PDF TO WORD task |
| 92 | +$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD); |
| 93 | + |
| 94 | +// Upload files |
| 95 | +$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']); |
| 96 | + |
| 97 | +// execute Task |
| 98 | +$client->executeTask($taskInfo['taskId']); |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +## Get Task Info |
| 104 | + |
| 105 | +Request task status and file-related meta data based on the task ID. |
| 106 | + |
| 107 | +```php |
| 108 | +// Create a client |
| 109 | +$client = new CPDFClient('public_key', 'secret_key'); |
| 110 | + |
| 111 | +// Create a task |
| 112 | +// Create an example of a PDF TO WORD task |
| 113 | +$taskInfo = $client->createTask(CPDFConversion::PDF_TO_WORD); |
| 114 | + |
| 115 | +// Upload files |
| 116 | +$file = $client->addFile('test.pdf')->uploadFile($taskInfo['taskId']); |
| 117 | + |
| 118 | +// Execute Task |
| 119 | +$client->executeTask($taskInfo['taskId']); |
| 120 | + |
| 121 | +// Query TaskInfo |
| 122 | +$taskInfo = $client->getTaskInfo($taskInfo['taskId']); |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +## Samples |
| 128 | + |
| 129 | +See ***"Samples"*** folder in this folder. |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +## Related Resources |
| 134 | + |
| 135 | +* [ComPDFKit API Documentation](https://api.compdf.com/api/docs/introduction) |
0 commit comments