icontact / booxtreamclient
BooXtream webservice的简单客户端示例
Requires
- php: >=7.2.5
- guzzlehttp/guzzle: ^7.5
Requires (Dev)
- phpunit/phpunit: ^9.5.24
- roave/security-advisories: dev-latest
This package is not auto-updated.
Last update: 2024-09-24 04:04:48 UTC
README
PHP中用于与BooXtream webservice一起使用的客户端类。
- 具有将epub文件上传到BooXtream webservice的能力
- 或者指定存储在BooXtream存储服务器上的epub文件
- 可以返回epub文件、(转换后的)mobi文件或包含一个或多个下载链接的xml文件
通过Composer安装
BooXtreamClient可在Packagist上找到,只需将其添加到您的composer.json中
composer require icontact/booxtreamclient
或者
{ "require": { "icontact/booxtreamclient": "~2.0" } }
或者您可以直接下载包并运行composer install
来获取需求。
目前唯一的要求是PHP 7.2.5及以上版本和Guzzle。
如果您不想使用Composer,您将需要自行满足依赖关系。
用法
您需要
- 服务的用户名和API密钥
- 一组选项(参考BooXtream web服务API文档)
- epub文件或存储在BooXtream服务上的文件
类型参数可以是'epub'、'mobi'或'xml'。在前两种情况下,服务将返回文件,在'xml'的情况下,您将根据您的设置(即交付平台)接收一个或两个下载链接。
require('vendor/autoload.php'); use \Icontact\BooXtreamClient\BooXtreamClient; use \Icontact\BooXtreamClient\Options; use \GuzzleHttp\Client; // a guzzle client $guzzle = new Client(); // an options object, refer to the documentation for more information on the options $options = new Options([ 'customername' => 'Foo Bar', 'referenceid' => 'bazdibzimgir12345', 'languagecode' => 1033 ]); // create the BooXtream Client $type = 'xml'; // returns downloadlinks $credentials = ['username', 'apikey']; // you will need a username and an API key $BooXtream = new BooXtreamClient($type, $options, $credentials, $guzzle);
现在我们将发送请求。
// add a file location to the epub file you want to watermark $BooXtream->setEpubFile('/path/to/your/file.epub'); // and send $Response = $BooXtream->send();
使用存储文件的请求略有不同。您只需提供文件名(带或不带.epub扩展名),而不是添加epub文件
$BooXtream->setStoredEpubFile('filename');
选项
可用的选项如下。有关详细信息,请参阅API文档
必需
- customername(字符串)
- customeremailaddress(字符串)
- referenceid(字符串)
- languagecode(整数)
如果使用xml(即交付平台)则额外必需
- expirydays(整数)
- downloadlimit(整数)
可选
- exlibrisfont(字符串),应包含'sans'、'serif'或'script'之一
- exlibris(布尔值),默认:false
- chapterfooter(布尔值),默认:false
- disclaimer(布尔值),默认:false
- showdate(布尔值),默认:false
如果使用xml(即交付平台)则可选
- epub(布尔值),默认:true
- kf8mobi(布尔值),默认:false
自定义ex libris
您还可以根据API文档中的说明设置自定义的ex libris文件。
$BooXtream->setExlibrisFile('filename');
响应
BooXtreamClient返回类型为GuzzleHttp\Psr7\Response的对象
响应始终包含状态码($Response->getStatusCode();
)。如果请求成功,则此值将为200。任何其他状态码都是错误,并将抛出类型为GuzzleHttp\Exception\ClientException的异常。您可以通过以下方式获取响应对象
try { $Response = $BooXtream->send(); } catch (ClientException $e) { $Response = $e->getResponse(); }
检查HTTP原因($Response->getReasonPhrase();
)以获取更多信息。
epub/mobi
如果您请求了epub或mobi文件,可以通过读取主体($Response->getBody();
)来访问它。主体是一个流,有关如何访问它的更多信息,请参阅PHP文档。此外,您可以使用$Response->getHeader('content-type');
访问文件的内容类型。
XML或错误
如果您请求了xml(即交付平台)或发生了错误,可以通过访问主体($Response->getBody();
)来获取更多信息。主体是一个流,有关如何访问它的更多信息,请参考PHP 文档。
XML看起来像这样
- 请求,包含您所做的请求的信息(选项等)
- 响应,包含一组下载链接或有关错误的更多信息