这是 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