apiera/woo-php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

apiera/woo-php-sdk

最新稳定版本:0.1.0

Composer 安装命令:

composer require apiera/woo-php-sdk

包简介

Woo Rest API PHP SDK composer library

README 文档

README

A lightweight PHP SDK for interacting with the WooCommerce REST API.

Requirements

  • PHP 8.3 or higher
  • A WooCommerce site with REST API enabled
  • API consumer key and secret from WooCommerce

Installation

Install via Composer:

composer require apiera/woo-php-sdk

Basic Usage

use Apiera\WooPhpSdk\Client;
use Apiera\WooPhpSdk\Configuration;
use Apiera\WooPhpSdk\Enum\ApiVersion;

// Create configuration
$config = new Configuration(
    baseUrl: 'https://your-store.com',
    consumerKey: 'your_consumer_key',
    consumerSecret: 'your_consumer_secret',
    apiVersion: ApiVersion::V3,
    userAgent: 'My Application/1.0',
    timeout: 30
);

// Initialize client
$client = new Client($config);

try {
    // Get all products
    $products = $client->get('products');

    // Create a product
    $newProduct = $client->post('products', [
        'name' => 'Test Product',
        'type' => 'simple',
        'regular_price' => '21.99'
    ]);

    // Update a product
    $updatedProduct = $client->put('products/123', [
        'name' => 'Updated Product Name'
    ]);

    // Delete a product
    $result = $client->delete('products/123');

} catch (\Apiera\WooPhpSdk\Exception\Http\HttpException $e) {
    $error = $e->getErrorMessage();
    echo sprintf(
        "Error: [%s] %s",
        $error->getCode(),
        $error->getMessage()
    );
}

Error Handling

The SDK throws specialized exceptions for different HTTP error scenarios:

  • BadRequestException - 400 errors
  • UnauthorizedException - 401 errors
  • NotFoundException - 404 errors
  • InternalServerErrorException - 500 errors
  • RequestException - Other HTTP errors

All exceptions extend from HttpException which provides helpful methods to access request/response details:

try {
    $client->get('products/999');
} catch (\Apiera\WooPhpSdk\Exception\Http\HttpException $e) {
    echo $e->getRequestMethod(); // GET
    echo $e->getRequestUri(); // products/999
    echo $e->getResponseStatusCode(); // 404
    
    $error = $e->getErrorMessage();
    echo $error->getCode(); // not_found
    echo $error->getMessage(); // Product not found
    print_r($error->getData()); // ['status' => 404]
}

Development

Run tests:

composer test

Run coding standards check:

composer cs:check

Run static analysis:

composer static:analyse

Run all checks:

composer check

License

MIT License - see the LICENSE file for details.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-02-14