ikurcubic/wolt-api

Wolt API 的 PHP 客户端

v1.0.2 2022-06-24 15:13 UTC

This package is auto-updated.

Last update: 2024-09-24 20:35:50 UTC


README

GitHub Workflow Status (master) Total Downloads Latest Version License

此包提供了用于项目和信息更新的 Wolt API PHP 客户端

安装

composer require ikurcubic/wolt-api

用法

    <?php
    use GuzzleHttp\Exception\ClientException;
    use IvanKurcubic\WoltAPI\Client;
    use IvanKurcubic\WoltAPI\Exceptions\HttpResponseException;

    $username = 'test';
    $password = 'b77b7a63cd5176154ca2802f9927ee598dc';
    $venueId = '62b041691ce47b414960c712'
    
    $api = new Client($username, $password, Client::ENV_STAGE);
    
    $data = [
        "data" => [
            ['sku'=>'1234', 'price'=>10000, 'enabled'=>true, 'vat_percentage'=>20.00],
            ['sku'=>'5678', 'price'=>20000, 'enabled'=>true, 'vat_percentage'=>20.00],
            ...
        ]   
    ];
    
    try {
        $api->updateItems($venueId, $data);
    } catch (HttpResponseException|ClientException $exception) {
        if ($exception->getResponse()->getStatusCode() == 429) {
            $retryAfter = $exception->getResponse()->getHeader('retry-after');
            if (is_array($retryAfter)) {
                $retryAfter = $retryAfter[0] ?? 0;
            }
            echo "Too many requests, need to wait $retryAfter seconds.";
            sleep($retryAfter + 2);
            $api->updateItems($venueId, $data);
        } else {
            throw $exception;
        }
    }    

Wolt API PHP 客户端Ivan Kurcubic 创建,采用 MIT 许可证