這是 CloudConvert API v2的官方 PHP SDK。
要安裝 PHP SDK,您需要在專案中使用 Composer。
與 Guzzle 一起安裝 SDK:
composer require cloudconvert/cloudconvert-php guzzlehttp/guzzle
透過使用 PSR-7、PSR-17、PSR-18 和 HTTPlug,此套件不會與任何特定的 HTTP 用戶端綁定。因此,您還需要安裝提供psr/http-client-implementation
和psr/http-factory-implementation
的軟體包(例如 Guzzle)。
use CloudConvert CloudConvert ;
use CloudConvert Models Job ;
use CloudConvert Models Task ;
$ cloudconvert = new CloudConvert ([
' api_key ' => ' API_KEY ' ,
' sandbox ' => false
]);
$ job = ( new Job ())
-> setTag ( ' myjob-1 ' )
-> addTask (
( new Task ( ' import/url ' , ' import-my-file ' ))
-> set ( ' url ' , ' https://my-url ' )
)
-> addTask (
( new Task ( ' convert ' , ' convert-my-file ' ))
-> set ( ' input ' , ' import-my-file ' )
-> set ( ' output_format ' , ' pdf ' )
-> set ( ' some_other_option ' , ' value ' )
)
-> addTask (
( new Task ( ' export/url ' , ' export-my-file ' ))
-> set ( ' input ' , ' convert-my-file ' )
);
$ cloudconvert -> jobs ()-> create ( $ job )
您可以使用 CloudConvert Job Builder 查看各種任務類型的可用選項。
上傳到 CloudConvert 是透過import/upload
任務完成的(請參閱文件)。這款SDK提供了一種便捷的上傳方式:
use CloudConvert Models Job ;
use CloudConvert Models Task ;
$ job = ( new Job ())
-> addTask ( new Task ( ' import/upload ' , ' upload-my-file ' ))
-> addTask (
( new Task ( ' convert ' , ' convert-my-file ' ))
-> set ( ' input ' , ' upload-my-file ' )
-> set ( ' output_format ' , ' pdf ' )
)
-> addTask (
( new Task ( ' export/url ' , ' export-my-file ' ))
-> set ( ' input ' , ' convert-my-file ' )
);
$ job = $ cloudconvert -> jobs ()-> create ( $ job );
$ uploadTask = $ job -> getTasks ()-> whereName ( ' upload-my-file ' )[ 0 ];
$ cloudconvert -> tasks ()-> upload ( $ uploadTask , fopen ( ' ./file.pdf ' , ' r ' ), ' file.pdf ' );
upload()
方法接受字串、PHP 資源或 PSR-7 StreamInterface
作為第二個參數。
您也可以直接允許客戶端將檔案上傳到CloudConvert:
< form action =" <?=$uploadTask->getResult()->form->url?> "
method =" POST "
enctype =" multipart/form-data " >
< ? foreach ((array)$uploadTask- > getResult()- > form- > parameters as $parameter = > $value) { ? >
< input type =" hidden " name =" <?=$parameter?> " value =" <?=$value?> " >
< ? } ? >
< input type =" file " name =" file " >
< input type =" submit " >
</ form >
CloudConvert 可以產生公用 URL 以使用export/url
任務。作業完成後,您可以使用 PHP SDK 下載輸出檔。
$ cloudconvert -> jobs ()-> wait ( $ job ); // Wait for job completion
foreach ( $ job -> getExportUrls () as $ file ) {
$ source = $ cloudconvert -> getHttpTransport ()-> download ( $ file -> url )-> detach ();
$ dest = fopen ( ' output/ ' . $ file -> filename , ' w ' );
stream_copy_to_stream ( $ source , $ dest );
}
download()
方法傳回 PSR-7 StreamInterface
,可以使用detach()
將其用作 PHP 資源。
可以在 CloudConvert 儀表板上建立 Webhooks,您也可以在那裡找到所需的簽署金鑰。
$ cloudconvert = new CloudConvert ([
' api_key ' => ' API_KEY ' ,
' sandbox ' => false
]);
$ signingSecret = ' ... ' ; // You can find it in your webhook settings
$ payload = @ file_get_contents ( ' php://input ' );
$ signature = $ _SERVER [ ' HTTP_CLOUDCONVERT_SIGNATURE ' ];
try {
$ webhookEvent = $ cloudconvert -> webhookHandler ()-> constructEvent ( $ payload , $ signature , $ signingSecret );
} catch ( CloudConvert Exceptions UnexpectedDataException $ e ) {
// Invalid payload
http_response_code ( 400 );
exit ();
} catch ( CloudConvert Exceptions SignatureVerificationException $ e ) {
// Invalid signature
http_response_code ( 400 );
exit ();
}
$ job = $ webhookEvent -> getJob ();
$ job -> getTag (); // can be used to store an ID
$ exportTask = $ job -> getTasks ()
-> whereStatus ( Task :: STATUS_FINISHED ) // get the task with ' finished ' status . . .
-> whereName ( ' export-it ' )[ 0 ]; // ... and with the name 'export-it'
// ...
或者,您可以使用 PSR-7 RequestInterface
來建構WebhookEvent
:
$ webhookEvent = $ cloudconvert -> webhookHandler ()-> constructEventFromRequest ( $ request , $ signingSecret );
簽名 URL 允許僅使用 URL 查詢參數按需轉換檔案。 PHP SDK 允許產生此類 URL。因此,您需要在 CloudConvert Dashboard 上取得簽名 URL 庫和簽署金鑰。
$ cloudconvert = new CloudConvert ([
' api_key ' => ' API_KEY ' ,
' sandbox ' => false
]);
$ job = ( new Job ())
-> addTask (
( new Task ( ' import/url ' , ' import-my-file ' ))
-> set ( ' url ' , ' https://my.url/file.docx ' )
)
-> addTask (
( new Task ( ' convert ' , ' convert-my-file ' ))
-> set ( ' input ' , ' import-my-file ' )
-> set ( ' output_format ' , ' pdf ' )
)
-> addTask (
( new Task ( ' export/url ' , ' export-my-file ' ))
-> set ( ' input ' , ' convert-my-file ' )
);
$ signedUrlBase = ' SIGNED_URL_BASE ' ;
$ signingSecret = ' SIGNED_URL_SIGNING_SECRET ' ;
$ url = $ cloudConvert -> signedUrlBuilder ()-> createFromJob ( $ signedUrlBase , $ signingSecret , $ job , ' CACHE_KEY ' );
預設情況下,使用您帳戶設定中的區域。或者,您可以設定固定區域:
// Pass the region to the constructor
$ cloudconvert = new CloudConvert ([
' api_key ' => ' API_KEY ' ,
' sandbox ' => false ,
' region ' => ' us-east '
]);
vendor/bin/phpunit --testsuite unit
vendor/bin/phpunit --testsuite integration
預設情況下,這會使用官方 CloudConvert 帳戶針對 Sandbox API 執行整合測試。如果您想使用自己的帳戶,可以使用CLOUDCONVERT_API_KEY
環境變數設定 API 金鑰。在這種情況下,您需要將 Sandbox API 的以下 MD5 雜湊值列入白名單(使用 CloudConvert 儀表板)。
53d6fe6b688c31c565907c81de625046 input.pdf
99d4c165f77af02015aa647770286cf9 input.png