motveu/php-api-sdk

此软件包最新版本(v1.3.2)没有提供许可证信息。

PHP SDK,用于与JACON SMS和moTV.eu中间件集成

v1.3.2 2024-01-04 08:14 UTC

This package is auto-updated.

Last update: 2024-09-04 09:51:04 UTC


README

此SDK可以用于与moTV.eu(https://motv.eu)和JACON CSMS(https://jacon.cz)集成。

安装

推荐的安装方式是通过Composer

composer require motveu/php-api-sdk

用法

用法非常简单。只需包含Composer的自动加载器,然后开始使用即可。

示例短信命令

有关完整的API文档和测试API端点,请与JACON(或如果与moTV.eu中间件捆绑,请与moTV.eu)联系。

<?php

require_once __DIR__ . '/vendor/autoload.php';

$smsConnector = new \Motv\Connector\Sms\AdminConnector('https://sms.operator.tv', 'Username', 'secret...');

// create sample customer
$viewersId = $smsConnector->Integration()->createMotvCustomer('test', 'myPassword');

// check whether there are any allowed products for the created customer
$allowedProducts = $smsConnector->Sales()->getAllowedProductsForCustomer($viewersId);

if (count($allowedProducts) === 0) {
	echo "No allowed products found!";
	
	exit(1);
}

// in a real world, it will not be the first product but rather whatever the customer chooses on the selfcare website for example
$products_id = array_pop($allowedProducts)['products_id'];

// subscribe to first product
$smsConnector->Integration()->subscribe($viewersId, $products_id);

// and now let's cancel the subscription
$smsConnector->Integration()->cancel($viewersId, $products_id);

// get all subscription info
$smsConnector->Subscription()->getCustomerSubscriptionInfo($viewersId);

// retrieve all data that are saved about the customer (personal data, contacts, addresses, devices)
$fullCustomerData = $smsConnector->Customer()->getData($viewersId);

// get the moTV.eu device number, this number can be used when querying the MW API
$motvDeviceId = $fullCustomerData['devices'][0]['device_motv_motv_id'];

// clean up after our tests
$smsConnector->Customer()->deleteCustomer($viewersId);

示例中间件命令

有关完整的API文档和测试API端点,请与moTV.eu联系。

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mwAdminConnector = new \Motv\Connector\Mw\AdminConnector('https://mw.operator.tv', 'Username', 'secret');

// get customer with internal ID 1
$customerEntity = $mwAdminConnector->Customer()->getData(1);
$vendorsPairs = $mwAdminConnector->Vendor()->getPairs();
echo 'Login: ' . $customerEntity->customers_login;
echo PHP_EOL;
echo 'Vendor: ' . $vendorsPairs[$customerEntity->customers_vendors_id];

支持实体、更新、获取数据和选择

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mwAdminConnector = new \Motv\Connector\Mw\AdminConnector('https://mw.operator.tv', 'Username', 'secret');

// It creates input entity and fills it with data
$personInputEntity = new \Motv\Connector\Mw\InputEntities\Mw\PersonEntity();
$personInputEntity->persons_type = \Motv\Connector\Mw\Enums\Mw\PersonEnum::ACTOR;
$personInputEntity->persons_birthday = '1990-01-01';
// in case of date, both string and DateTime objects are accept
$personInputEntity->persons_birthday = (new \DateTimeImmutable)->setTimestamp(strtotime('now'));
$personInputEntity->persons_description = 'Popular actor';
$personInputEntity->persons_name = 'John Smith';

// Creates new Person
$personsId = $mwAdminConnector->Person()->update(null, $personInputEntity);

// Gets the new person we just created
$personEntity = $mwAdminConnector->Person()->getData($personsId);
echo 'Actor ' . $personEntity->persons_name . ' with ID: ' . $personEntity->persons_id;
echo PHP_EOL;

// Let's change name of the Person
$personInputEntity->persons_name = 'Will Smith';

// Updates the name
$mwAdminConnector->Person()->update($personsId, $personInputEntity);

// Let's double-check that the changes were actually reflected
$personEntity = $mwAdminConnector->Person()->getData($personsId);
echo 'Actor ' . $personEntity->persons_name . ' with ID: ' . $personEntity->persons_id;
echo PHP_EOL;

// Select the person by selection function
$selectedActorEntity = $mwAdminConnector->Person()->selection(['persons_name' => 'Will Smith'])['rows'][0];
echo 'Actor ' . $selectedActorEntity->persons_name . ' with ID: ' . $selectedActorEntity->persons_id;
echo PHP_EOL;

// sending invalid data will result into a neat error, for example
$personInputEntity = new \Motv\Connector\Mw\InputEntities\Mw\PersonEntity();
$personInputEntity->persons_name = '';
$personInputEntity->persons_type = \Motv\Connector\Mw\Enums\Mw\PersonEnum::ACTOR;
$personInputEntity->persons_birthday = '1990-01-01';
$personInputEntity->persons_description = 'Popular actor';

// Will not create a new person because name is empty, will throw an exception instead
$personsId = $mwAdminConnector->Person()->update(null, $personInputEntity);

moTV VOD文件上传

文件大小超过20MB无法通过GUI或直接通过API上传,因为文件大小有限制。需要分块上传。我们已准备了一个示例脚本VodUpload.php。请注意,该脚本使用shell工具split将原始文件分割成块,因此它不适用于Windows操作系统。

<?php

require_once __DIR__ . '/vendor/autoload.php';

$vodUpload = new \Motv\Connector\Mw\VodUpload('https://mw.operator.tv', 'Username', 'secret', 'https://storage.operator.tv', 'temp_path');

$vodsId = 1;
$this->vodUpload->uploadVodFile($vodsId, '/path/test.mp4');

捕获错误

可能会在出现意外问题时抛出异常,例如客户不存在。还有可能抛出UnknownApiException,当发生不可预测的情况时。当连接出现问题时,还会抛出标准Guzzle(https://github.com/guzzle/guzzle)异常。

try { 
	$fullCustomerData = $mwAdminConnector->Customer()->getData($customersId);
} catch (\Motv\Mw\CustomerUnknownException $e) {
	echo "The customer does not exist!";	
} catch (\Motv\Mw\UnknownApiException $e) {
	echo "Something went really wrong!"
} catch (\GuzzleHttp\Exception\TransferException $e) {
	echo "Some connection issues!";
}

日志记录

此SDK附带Monolog(《https://github.com/Seldaek/monolog》)支持。您可以将日志记录器传递给$connector->setLogger()方法,并记录所有API通信。

$mwAdminConnector = new \Motv\Connector\Mw\AdminConnector('https://mw.operator.tv', 'Username', 'secret');

$logger = new \Monolog\Logger('API');
$logger->pushHandler(new \Monolog\Handler\RotatingFileHandler('api.log', 14));
$logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout'));

$mwconnector->setLogger($logger);

专业提示

使用现代IDE,如PHP Storm。它将为您提供方法名称以及参数名称和类型的提示。