qaamgo / onlineconvert-api-sdk
使用 Online Convert Api 版本 2 的 Sdk
Requires
- php: ^7.1|^8.0
- guzzlehttp/guzzle: ^6.5.7|^7.4.4
Requires (Dev)
- phpunit/phpunit: ~7.5 || ^9.3
- symfony/filesystem: ^3
- symfony/finder: ^3
This package is auto-updated.
Last update: 2024-09-27 14:55:52 UTC
README
此 SDK 提供了一个代码库,用于与 Online-Convert.com 的 API 版本 2 进行交互。
安装
推荐的安装方式是通过 Composer。
composer require qaamgo/onlineconvert-api-sdk
入门指南
配置
require 'vendor/autoload.php'; $config = new \OnlineConvert\Configuration(); $config->setApiKey('main', 'HERE YOUR API KEY'); $client = new \OnlineConvert\Client\OnlineConvertClient($config, 'main'); $syncApi = new \OnlineConvert\Api($client); $asyncApi = new \OnlineConvert\Api($client, true);
发送完整任务
$syncJob = [ 'input' => [ [ 'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_UPLOAD, 'source' => '~/example.png' ], [ 'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_REMOTE, 'source' => 'http://cdn.online-convert.com/images/logo-top.png' ] ], 'conversion' => [ [ 'target' => 'png' ], [ 'target' => 'mp4' ] ] ]; $asyncJob = [ 'input' => [ [ 'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_UPLOAD, 'source' => '~/example.png' ], [ 'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_REMOTE, 'source' => 'http://cdn.online-convert.com/images/logo-top.png' ] ], 'conversion' => [ [ 'target' => 'png' ], [ 'target' => 'mp4' ] ], 'callback' => 'http://alert.me/when/job/is/finished' ]; $syncJob = $syncApi->postFullJob($syncJob)->getJobCreated(); $asyncJob = $asyncApi->postFullJob($asyncJob)->getJobCreated(); var_dump($syncJob, $asyncJob);
使用 gdrive_picker 输入发送任务
什么是 Google Drive Picker
Google Drive Picker 允许您访问存储在您的 Google Drive 账户中的文件。
您可以在官方页面上找到有关它的信息。
每个字段的含义:
- type
- 指定我们想要使用 Google Drive Picker 输入
- source
- 这必须包含 Google Drive Picker 返回的 文件 ID
- credentials
- 这必须是一个包含所选文件凭据的数组
- 目前您只需传递其中的 "token" 字段
- "token" 字段是当选择文件时 Google Drive Picker 返回的
- content_type
- 当您选择 Google 文档 时,此字段是必填的
- 如果您选择其他类型的文件,则可以留空此字段
- 例如:pdf,png,zip...
- filename
- 这是 Google Drive Picker 文件的文件名
- 如果此字段未发送,则文件将使用
output.<extension>
的名称下载
$job = [
'input' => [
[
'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_GDRIVE_PICKER,
'source' => '<put google drive picker file id here>',
'credentials' => [
'token' => '<put google drive picker token for the selected file here>'
],
'content_type' => '<if this is a specific google document>',
'filename' => '<name of the input file>'
]
],
'conversion' => [
[
'target' => 'png'
]
]
];
之后,只需按照如何有效地将任务发送到 API 的先前示例进行操作。
使用云存储提供商发送任务
以下是一个示例,说明如何发送一个任务,其中我们想要转换存储在 Amazon S3 存储中的文件。
如果您想了解更多信息,请查看我们的云存储 API 文档
$job = [
'input' => [
[
'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_CLOUD,
'source' => 'amazons3',
'parameters' => [
'bucket' => 'your.bucket.name',
'file' => 'the complete path to the file',
],
'credentials' => [
'accesskeyid' => 'your access key id',
'secretaccesskey' => 'your secret access key',
]
]
],
'conversion' => [
[
'target' => 'png'
]
]
];
之后,只需按照如何有效地将任务发送到 API 的先前示例进行操作。
下载转换后的文件
您可以使用以下代码片段下载转换后的文件
require_once __DIR__ . '/vendor/autoload.php';
$config = new \OnlineConvert\Configuration();
$config->setApiKey('main', 'PUT YOUR API KEY HERE');
// Remember to specify your own downloads folder.
$config->downloadFolder = __DIR__ . '/downloads';
$client = new \OnlineConvert\Client\OnlineConvertClient($config, 'main');
$syncApi = new \OnlineConvert\Api($client);
$outputEndpoint = $syncApi->getOutputEndpoint();
$syncJob = [
'input' => [
[
'type' => \OnlineConvert\Endpoint\InputEndpoint::INPUT_TYPE_REMOTE,
'source' => 'PUT URL HERE'
]
],
'conversion' => [
[
'target' => 'jpg'
]
]
];
$job = $syncApi->postFullJob($syncJob)->getJobCreated();
// You will find the file/s in the downloads folder
$outputEndpoint->downloadOutputs($job);
高级使用
类 \OnlineConvert\Api::class 有一些快捷方法,将其链接到端点类 (例如:\OnlineConvert\Api::postFullJob()),但从该类中可以调用实际端点和它们的方法。
此外,如果您喜欢,可以逐个创建端点类。为此,请检查 \OnlineConvert\Endpoint\Abstracted::_construct()
重要:类 \OnlineConvert\Endpoint\JobsEndpoint 可用于发送同步和异步任务;请检查 \OnlineConvert\Endpoint\JobsEndpoint::setAsync()
您还可以设置客户端的不同头信息,并使用任务令牌执行某些操作。
$config = new \OnlineConvert\Configuration();
$client = new \OnlineConvert\Client\OnlineConvertClient($config);
$syncApi = new \OnlineConvert\Api($client);
$ep = $syncApi->getJobsEndpoint();
//Option 1
$ep->getClient()->setHeader(\OnlineConvert\Client\Interfaced::HEADER_OC_API_KEY, 'YOUR API KEY');
//Option 2
//$client->setHeader(\OnlineConvert\Client\Interfaced::HEADER_OC_API_KEY, 'YOUR API KEY');
//$ep->setClient($client);
$job = $ep->postJob([]);
$ip = $syncApi->getInputEndpoint();
//WORK WITH TOKENS
//OPTION 1
$input = $ip->setUserToken($a['token'])->postJobInputRemote('http://www.online-convert.com/', $a['id']);
//Option 2 (Apply also the previous options to set a header)
//$ip->getClient()->setHeader(\OnlineConvert\Client\Interfaced::HEADER_OC_JOB_TOKEN, $a['token']);
//$b = $ip->postJobInputRemote('http://www.online-convert.com/', $a['id']);
var_dump($job, $input);
管理异常
如果您捕获 \OnlineConvert\Exception\OnlineConvertSdkException::class,则自动捕获此 SDK 中的所有异常
获取上传文件的 URL
- 发送任务请求
- 从任务中获取以下键:服务器 和 id。您可以使用 \OnlineConvert\Api 或 \OnlineConvert\Endpoint\JobsEndpoint 获取任务信息。您将得到如下信息:
[
[id] => 00000000-0000-0000-0000-000000000000
[token] => some_token_here
...
[server] => http://www9.online-convert.com/dl
...
]
- 有了这些信息,您可以进行如下调用:
\OnlineConvert\Client\OnlineConvertClient->generateUrl(\OnlineConvert\Endpoint\Resources::URL_POST_FILE, ['server' => $server, 'job_id' => $jobId])
- 此外,这些令牌还可以用于您可能希望与您的用户共享的一些操作。
通过AJAX直接将文件上传到Online-Convert.com服务器
实际案例:我想直接将我们的用户文件发送到API,因为先发送到我的应用程序然后再发送到API会花费太多时间。
考虑因素:通常,这将节省我们处理时间、硬盘空间并提高用户体验,但建议有一个无需使用JS的备用方案。
以下示例使用的是JQuery。
重要:上传操作必须逐个进行。因此,您需要为每个文件创建一个表单。
HTML代码
<form id="postFile" enctype="multipart/form-data" method="post" action="javascript:;" accept-charset="utf-8"> <input name="file" type="file" id="file"/> <input type="submit" id="upload-btn"/> </form>
JS代码
$(document).ready(function () { $('#postFile').submit(function (event) { event.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: 'URL_GENERATED_TO_UPLOAD_FILE', type: 'POST', data: formData, async: false, cache: false, processData: false, contentType: false, mimeType: 'multipart/form-data', headers: { 'x-oc-token': 'JOB_TOKEN_HERE', 'cache-control': 'no-cache' }, success: function () { window.alert('Success'); }, error: function () { window.alert('Error'); } }); }); });