claassenmarius/php-skynet

一个用于使用 Skynet Courier API 的 PHP 包。

1.0.0 2021-06-23 14:20 UTC

This package is auto-updated.

Last update: 2024-09-24 23:57:30 UTC


README

Tests Check & fix styling

一个不依赖于框架的 PHP 包,用于使用 Skynet Courier API。

安装

使用 composer 安装此包

composer require claassenmarius/php-skynet

用法

创建一个 Claassenmarius\PhpSkynet\Skynet 实例,传入您的 Skynet 用户名、密码、账户号和系统 ID。

use Claassenmarius\PhpSkynet\Skynet;

$skynet = new Skynet(
  'skynet_username',
  'skynet_password',
  'skynet_system_id',
  'skynet_account_number'
);

以下方法可用于获取安全令牌、验证郊区/邮政编码组合、获取郊区邮政编码列表、获取包裹报价、获取两个地点之间的预计时间、生成运单、获取运单 POD 图片和跟踪运单。每个方法都返回一个新的 Claassenmarius\PhpSkynet\Response 对象,该对象提供了检查响应的方法。

获取安全令牌

$response = $skynet->securityToken();

验证郊区/邮政编码组合

$response = $skynet->validateSuburbAndPostalCode([
    'suburb' => 'Brackenfell',
    'postal-code' => '7560'
]);

获取郊区邮政编码列表

$response = $skynet->postalCodesFromSuburb('Brackenfell');

获取包裹报价

$response = $skynet->quote([
    'collect-city' => 'Brackenfell',
    'deliver-city' => 'Stellenbosch',
    'deliver-postcode' => '7600',
    'service-type' => 'ON1',
    'insurance-type' => '1',
    'parcel-insurance' => '0',    
    'parcel-length' => 10, //cm
    'parcel-width' => 20, // cm
    'parcel-height' => 30, //cm
    'parcel-weight' => 20 //kg
]);

获取两个地点之间的预计时间

$response = $skynet->deliveryETA([
    'from-suburb' => 'Brackenfell',
    'from-postcode' => '7560',
    'to-suburb' => 'Stellenbosch',
    'to-postcode' => '7600',
    'service-type' => 'ON1'
]);

生成运单

$response = $skynet->createWaybill([
    "customer-reference" => "Customer Reference",
    "GenerateWaybillNumber" => true,
    "service-type" => "ON1",
    "collection-date" => "2021-06-26",
    "from-address-1" => "3 Janie Street, Ferndale, Brackenfell",
    "from-suburb" => "Brackenfell",
    "from-postcode" => "7560",
    "to-address-1" => "15 Verreweide Street, Universiteitsoord, Stellenbosch",
    "to-suburb" => "Stellenbosch",
    "to-postcode" => "7600",
    "insurance-type" => "1",
    "insurance-amount" => "0",
    "security" => "N",
    "parcel-number" => "1",
    "parcel-length" => 10,
    "parcel-width" => 20,
    "parcel-height" => 30,
    "parcel-weight" => 10,
    "parcel-reference" => "12345",
    "offsite-collection" => true
]);

获取运单 POD 图片

$response = $skynet->waybillPOD('your-waybill-number');

跟踪运单

$response = $skynet->trackWaybill('your-waybill-number');

响应

Claassenmarius\PhpSkynet\Response 提供以下方法来检查响应。

以字符串格式获取响应体

$securityToken = $response->body(); 
// "{"SecurityToken":"2_f77e4922-1407-485e-a0fa-4fdd5c29e9ca"}" 

获取响应的 JSON 解码体,作为数组或标量值

$securityToken = $response->json(); 
// ["SecurityToken" => "2_c767aa41-bca8-4084-82a0-69d8e27fba2c"] 

获取响应的 JSON 解码体,作为对象。

$securityToken = $response->object(); 
// { +"SecurityToken": "2_c767aa41-bca8-4084-82a0-69d8e27fba2c" }

获取响应中的标题

$header = $response->header('Content-Type'); 
// "application/json; charset=utf-8"

获取响应中的所有标题

$headers = $response->headers(); 
// Return an array of all headers

获取响应的状态码

$headers = $response->status(); 
// 200

确定请求是否成功(状态码是否 >= 200 且 < 300)

$headers = $response->successful(); 
// true

确定响应代码是否为 "OK"。 (状态码 === 200)

$headers = $response->ok(); 
// true

确定是否发生服务器错误。 (状态码 >= 500)

$headers = $response->serverError(); 
// false

确定是否发生客户端或服务器错误。

$headers = $response->failed(); 
// false

异常处理

此包在后台使用 Guzzle PHP HTTP 客户端 发送请求。

  • 在发生网络错误(连接超时、DNS 错误等)的情况下,会抛出 GuzzleHttp\Exception\RequestException 异常。此异常扩展自 GuzzleHttp\Exception\TransferException。捕获此异常将捕获在传输请求时可能抛出的任何异常。
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;

try {
    $response = $skynet->securityToken();
} catch(RequestException $e) {
    // Handle the exception
}
  • 在发生网络错误的情况下,会抛出 GuzzleHttp\Exception\ConnectException 异常。此异常扩展自 GuzzleHttp\Exception\TransferException。
  • 如果将 http_errors 请求选项设置为 true,则会抛出 GuzzleHttp\Exception\ClientException 异常。此异常扩展自 GuzzleHttp\Exception\BadResponseException,而 GuzzleHttp\Exception\BadResponseException 扩展自 GuzzleHttp\Exception\RequestException。
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\ClientException;

try {
    $response = $skynet->securityToken();
} catch(ClientException $e) {
    // Handle the exception
}
  • 如果将http_errors请求选项设置为true,则会在500级别错误时抛出GuzzleHttp\Exception\ServerException异常。此异常继承自GuzzleHttp\Exception\BadResponseException

测试

composer test

变更日志

请参阅变更日志获取有关最近变更的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件marius.claassen@outlook.com联系,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。