markosirec/gls-italy-sdk

GLS Italy 的 PHP SDK

v2.1.1 2022-02-20 13:34 UTC

This package is auto-updated.

Last update: 2024-09-20 20:16:10 UTC


README

这是一个为 GLS Italy Web 服务提供的非官方 PHP SDK。由于官方文档和 API 都使用意大利语,可能会非常混乱。因此,我决定创建一个简单的 SDK,以便您可以轻松地将它集成到现有的项目中。

请注意 - GLS Italy 拥有自己的独特 API,与其他国家的 GLS 分支有很大的不同。

新增功能

v2.0.0

现在可以批量添加和关闭包裹。您传递一个包裹实例数组,而不是为每个单独的包裹进行调用。(请参阅下面的示例)

入门指南

先决条件

  • PHP 7.1
  • PHP cURL 库

安装

直接克隆/下载此仓库,或者通过 composer 获取

composer require markosirec/gls-italy-sdk

使用方法

列出包裹

以下是如何在 GLS 系统中获取包裹列表的示例。这些端点有时会返回奇怪或不完整的结果 - 似乎是 GLS 系统中的错误。请记住,您的结果可能会有所不同!

$auth = new MarkoSirec\GlsItaly\SDK\Models\Auth();

$auth->setBranchId('your-branch-id');
$auth->setClientId('your-client-id');
$auth->setContractId('your-contract-id');
$auth->setPassword('your-password');

// List parcels for the last 40 days
$parcels = MarkoSirec\GlsItaly\SDK\Services\ParcelService::list($auth);

// List parcels by status (1 = closed parcels, 0 = waiting)
$parcels = MarkoSirec\GlsItaly\SDK\Services\ParcelService::listByStatus($auth, 1);

// List parcels by date/period (YYYYMMDD or YYYYMMDDHHII)
$parcels = MarkoSirec\GlsItaly\SDK\Services\ParcelService::listByPeriod($auth, "202001221300", "202001221700");

添加包裹并生成 PDF 标签/贴纸

有关完整选项列表,请参阅 src/Models/Parcel.php 中的包裹模型。

$auth = new MarkoSirec\GlsItaly\SDK\Models\Auth();

$auth->setBranchId('your-branch-id');
$auth->setClientId('your-client-id');
$auth->setContractId('your-contract-id');
$auth->setPassword('your-password');

$parcels = [];

$parcel = new MarkoSirec\GlsItaly\SDK\Models\Parcel();
$parcel->setName('John Smith');
$parcel->setAddress('Via su vrangone, 191');
$parcel->setCity('SOS ALINOS');
$parcel->setPostcode('08028');
$parcel->setProvince('NU');
$parcel->setWeight('2,7');
$parcel->setEmail('email@client.com');
$parcel->setOrderId(12345);
$parcels[] = $parcel;

$parcel = new MarkoSirec\GlsItaly\SDK\Models\Parcel();
$parcel->setName('Barbara Jordan');
$parcel->setAddress('Via Roma, 12');
$parcel->setCity('SOS ALINOS');
$parcel->setPostcode('08028');
$parcel->setProvince('NU');
$parcel->setWeight('2,1');
$parcel->setEmail('email2@client.com');
$parcel->setOrderId(12346);
$parcels[] = $parcel;

$result = MarkoSirec\GlsItaly\SDK\Services\ParcelService::add($auth, $parcels);

foreach ($result as $parcel) {

    if (empty($parcel->getError())) {

        // this is your tracking code/id. I suggest you save it!
        $parcelId = $parcel->getParcelId();

        // generate the PDF
        file_put_contents(
            $parcelId.'.pdf', 
            base64_decode($parcel->getPdfLabel())
        );
    }
}

try {

    // After the parcels have been added, the webservice needs you to confirm the shipping by calling the close endpoint. 
    // You can also do this later if you wish. This is the equivalent of the "CloseWorkDay" endpoint. 
    // You need to supply the parcels you want to "close".
    MarkoSirec\GlsItaly\SDK\Services\ParcelService::close($auth, $parcels);
}

catch(Exception $e) {

    // get the raw response error
    var_dump($e->getResponse());

    // get parsed xml from the GLS response (if the XML was parsed)
    var_dump($e->getXmlResponse());
}

删除包裹

$auth = new MarkoSirec\GlsItaly\SDK\Models\Auth();

$auth->setBranchId('your-branch-id');
$auth->setClientId('your-client-id');
$auth->setContractId('your-contract-id');
$auth->setPassword('your-password');

// this is the parcel id/tracking code you received from GLS when you added the parcel
$parcelId = 123;

// returns true on success, false on error
$result = MarkoSirec\GlsItaly\SDK\Services\ParcelService::delete($auth, $parcelId);

运行测试

SDK 使用 phpunit 进行测试。进入 SDK 根目录并运行

vendor/bin/phpunit tests

贡献

欢迎提交拉取请求。(请使用 PSR-2 编码风格)此库尚不支持某些 API 功能/端点,因此如果您有特定的需求,请与我联系,我将尽力帮助。

作者

联系方式

  • 如果您有任何问题或功能请求,请通过电子邮件 m.sirec@gmail.com 联系我
  • (抱歉,我不会说意大利语)

许可

此项目受 MIT 许可协议许可。