conversiontools / conversiontools-php
Conversion Tools REST API PHP 客户端库
1.0.3
2021-07-26 16:33 UTC
Requires
- php: >=5.4.0
- ext-curl: *
- ext-json: *
README
Conversion Tools 是一种在线服务,提供快速且简便的方式来在不同格式之间转换文档,如 XML、Excel、PDF、Word、Text、CSV 等。
此客户端允许将文件转换集成到 PHP 应用程序中。
PHP 客户端使用公共 Conversion Tools REST API 来转换文件。
安装
使用 Composer 安装
composer require conversiontools/conversiontools-php
或无需 Composer
require_once('conversiontools-php/src/autoload.php');
示例
要使用 REST API - 从 https://conversiontools.io/profile 的个人资料页面获取 API 令牌。
转换文件
查看 ./examples/
目录中的示例 convert-file.php
。
<?php require_once __DIR__ . '/vendor/autoload.php'; use \ConversionTools\ConversionClient; // put token here from your Profile page at https://conversiontools.io/profile $token = ''; $fileOrUrlInput = 'test.xml'; $fileOutput = 'test.csv'; $options = ['delimiter' => 'tabulation']; $client = new ConversionClient($token); try { $client->convert('convert.xml_to_csv', $fileOrUrlInput, $fileOutput, $options); } catch (Exception $e) { print 'Exception: ' . $e->getMessage() . "\n"; }
转换 URL
查看 ./examples/
目录中的示例 convert-url.php
。
<?php require_once __DIR__ . '/vendor/autoload.php'; use \ConversionTools\ConversionClient; // put token here from your Profile page at https://conversiontools.io/profile $token = ''; $fileOrUrlInput = 'https://google.com'; $fileOutput = 'result.pdf'; $options = []; $client = new ConversionClient($token); try { $client->convert('convert.website_to_pdf', $fileOrUrlInput, $fileOutput, $options); } catch (Exception $e) { print 'Exception: ' . $e->getMessage() . "\n"; }
API
使用令牌创建 ConversionClient
实例。
use \ConversionTools\ConversionClient; $client = new ConversionClient('<token>');
其中 <token>
是来自账户个人资料页面的 API 令牌 https://conversiontools.io/profile。
转换输入文件并下载结果
$client = new ConversionClient($token); try { $client->convert('<conversion type>', $fileOrUrlInput, $fileOutput, $options); } catch (Exception $e) { print 'Exception: ' . $e->getMessage() . "\n"; }
其中
<conversion type>
是特定的转换类型,请参阅 API 文档。$fileOrUrlInput
是输入文件的文件名,或要转换的文件的 URL(当适用时)$fileOutput
是输出文件的文件名$options
是一个 PHP 数组,包含相应转换器的选项,例如
$options = ['delimiter' => 'tabulation'];
需求
PHP 版本 5.4.0 或更高版本。
文档
可用的转换类型列表及其相应的转换选项可在 Conversion Tools API 文档 页面上找到。
许可
许可协议为 MIT。
版权所有 (c) 2021 Conversion Tools