基于 OAuth 的 UPS PHP SDK

dev-main 2024-05-30 18:49 UTC

This package is auto-updated.

Last update: 2024-09-30 19:31:33 UTC


README

UPS PHP SDK 通过 PHP 语言为应用程序提供方便的访问 UPS RESTful API 的方式。它包含一系列动态从 API 响应中初始化的 API 资源类集合,使其与 UPS API 的多个版本兼容。此 SDK 基于 官方 UPS API 文档仓库,并使用 Swagger-Codegen 工具 生成。

要求

PHP 7.4 及以上版本。

Composer

您可以通过 Composer 安装这些绑定。要安装所有支持的 API,请运行以下命令

composer require abantecart/ups-php

如果您只想安装部分 API,您应该在所选 API 目录中运行此命令。

要使用这些绑定,请使用 Composer 的 自动加载

require_once 'vendor/autoload.php';

依赖项

为了正常工作,这些绑定需要以下扩展

如果您使用 Composer,这些依赖项应该会自动处理。

入门

生成访问令牌

//YOUR ACCOUNT NUMBER (6 characters)
$accNumber = '******';
//UPS API Credentials (obtain after APP creation)
$clientId = '***YOUR_UPS_API_CLIENT_ID***';
$password = '***YOUR_UPS_API_PASSWORD***';

$config = \UPS\OAuthClientCredentials\Configuration::getDefaultConfiguration()
    ->setUsername($clientId)
    ->setPassword($password);

$apiInstance = new \UPS\OAuthClientCredentials\Request\DefaultApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
    new \GuzzleHttp\Client(),
    $config
);
$grant_type = "client_credentials"; // string |
$x_merchant_id = $accNumber; // string | Client merchant ID

try {
    $result = $apiInstance->createToken($grant_type, $x_merchant_id);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->generateToken: ', $e->getMessage(), PHP_EOL;
}

评分请求

// Configure OAuth2 access token for authorization: oauth2
$config = UPS\Rating\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');

$apiInstance = new UPS\Rating\Request\DefaultApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client(),
    $config
);
$body = new \UPS\Rating\Rating\RATERequestWrapper(); // \UPS\Rating\Rating\RATERequestWrapper | Generate sample code for popular API requests by selecting an example below. To view a full sample request and response, first click "Authorize" and enter your application credentials, then populate the required parameters above and click "Try it out".
$version = "v1"; // string | Indicates Rate API to display the new release features in Rate API response based on Rate release. See the New section for the latest Rate release. Supported values: v1, v1601, v1607, v1701, v1707, v2108, v2205. Length 5
$requestoption = "Shop"; // string | Valid Values: Rate = The server rates (The default Request option is Rate if a Request Option is not provided). Shop = The server validates the shipment, and returns rates for all UPS products from the ShipFrom to the ShipTo addresses. Rate is the only valid request option for Ground Freight Pricing requests. . Length 10
$trans_id = "trans_id_example"; // string | An identifier unique to the request. Length 32
$transaction_src = "testing"; // string | An identifier of the client/source application that is making the request.Length 512
$additionalinfo = ""; // string | Valid Values: timeintransit = The server rates with transit time information combined with requestoption in URL.Rate is the only valid request option for Ground Freight Pricing requests. Length 15

try {
    $result = $apiInstance->rate($body, $version, $requestoption, $trans_id, $transaction_src, $additionalinfo);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->rate: ', $e->getMessage(), PHP_EOL;
}

文档

查看 UPS API 文档