pavolbiely / neoship

dev-master 2019-11-08 16:14 UTC

This package is auto-updated.

Last update: 2024-09-09 02:52:22 UTC


README

Build Status Coverage Status Donate

请通过 Neoship 联系以获取访问其API的凭据。

安装

使用composer安装此软件包。

使用示例

创建一个新的Neoship API客户端实例

use Neoship\Neoship;
use Neoship\NeoshipException;

$clientId = ''; // client secret is provided from Neonus
$clientSecret = ''; // client secret is provided from Neonus
$redirectUri = 'https://...'; // URL where Neoship will redirect you after you authorize it to exchange OAuth code for an access token
$neoship = new Neoship($clientId, $clientSecret, $redirectUri);

要访问API,您必须首先获取一个OAuth代码,稍后将其交换为访问令牌。获取OAuth代码需要在Neoship网站上输入有效的凭据,您可以通过调用getAuthorizationUrl()方法来获取其URL。

$authUrl = $neoship->getAuthorizationUrl();
header('Location: ' . $authUrl);
exit;

在授权成功后,您将被重定向到$redirectUri变量中的URL。

try {
    if ($neoship->requestAccessToken($_GET['code'])) { // exchange OAuth code for an access token
        // we got the token
        print_r($neoship->getToken());
    }
} catch (NeoshipException $e) {
    // something went wrong
}

只要令牌有效,您就可以调用任何API调用。以下代码演示了如何发送一个简单的包裹。

// get all countries list
$countries = [];
foreach ($neoship->apiGetState() as $item) {
	$countries[$item->code] = $item->id;
}

// get all currencies list
$currencies = [];
foreach ($neoship->apiGetCurrency() as $item) {
	$currencies[$item->code] = new Currency($item->id, $item->name, $item->code, $item->symbol, $item->rate);
}

// prepare sender and recipient addresses
$sender = new Address('Harry Potter', 'Webtec', 'Magnetová 1', '831 04', 'Bratislava', $countries['SK']);
$recipient = new Address('Albus Dumbledore', 'Neonus', 'Miestneho priemyslu 1247', '029 01', 'Námestovo', $countries['SK']);

// create package
$package = new Package(123, $recipient, $sender, '1201800002');

// use the following code if the package is sent by cash on delivery payment
$cashOnDelivery = new Payment(10.0, $currencies['EUR'], Payment::TYPE_VIAMO);
$package->setCashOnDelivery($cashOnDelivery);

// use the following code if the package is insured
$insurance = new Payment(2500.0);
$package->setInsurance($insurance);

// send a package through the Neoship API
$neoship->apiPostPackage($package);

访问令牌通常有效期为1小时。如果令牌被发现无效,将抛出Neoship\NeoshipException异常。在这种情况下,必须从头开始重新获取访问令牌的整个过程。

API概述

如何运行测试?

测试是用 Nette Tester 构建的。您可以这样运行它

php -f tester ./ -c php.ini-mac --coverage coverage.html --coverage-src ../src

最小要求

  • PHP 7.1+
  • php-curl

许可证

MIT许可证 (c) Pavol Biely

读取提供的LICENSE文件以获取详细信息。