textalk/webshop-client

Textalk Webshop API客户端

0.3.0 2021-07-05 06:18 UTC

README

Build Status Coverage Status

一个用于简化Abicart API API使用的库。

示例

要获取Demoshop中前两个商品组的瑞典语(sv)名称,你可以这样做

<?php

require(dirname(dirname(__FILE__)) . '/vendor/autoload.php');

use Textalk\WebshopClient\Connection;

$api = Connection::getInstance('default', array('webshop' => 22222));

var_dump(
  $api->Articlegroup->list(
    array("name" => "sv", "uid" => true),
    array("limit" => 2)
  )
);

// Will produce:
// array(2) {
//   [0] =>
//   array(2) {
//     'name' =>
//     array(1) {
//       'sv' =>
//       string(4) "Herr"
//     }
//     'uid' =>
//     int(1347891)
//   }
//   [1] =>
//   array(2) {
//     'name' =>
//     array(1) {
//       'sv' =>
//       string(3) "Dam"
//     }
//     'uid' =>
//     int(1347897)
//   }
// }

你可以为特定实例保存API句柄

$api = Connection::getInstance('default', array('webshop' => 22222));

// Save a handle to the API for a single Articlegroup:
$articlegroup = $api->Articlegroup(1347891);

// Get all names:
// These are all equivalent:
//
//  * $api->Articlegroup(1347891)->get('name')
//  * $api->Articlegroup->get(1347891, 'name')
//  * $api->call('Articlegroup.get', array(1347891, 'name'))
var_dump(
  $articlegroup->get('name')
);

// Will produce:
// array(1) {
//   'name' =>
//   array(2) {
//     'en' =>
//     string(3) "Men"
//     'sv' =>
//     string(4) "Herr"
//   }
// }

如果你出错,你会得到特定的异常,并看到实际的请求

// This line won't actually DO anything, so it won't crasch:
$scissor = $api->IDontKnow("What I'm doing");

// But this will:
$scissor->run();

// -->
// PHP Fatal error:  Uncaught exception 'Textalk\WebshopClient\Exception\MethodNotFound' with message 'IDontKnow.run: Method not found: No API for IDontKnow
// On request: {"jsonrpc":"2.0","method":"IDontKnow.run","id":"7089b561-9252-4a0a-b45b-15a873509571","params":["What I'm doing"]}' in /home/liljegren/textalk-webshopclient-php/lib/Exception.php:32

命名连接

通常,你只希望在应用程序中有一个连接。你可以使用Connection::getInstance()并在每次都获得相同的Connection。

你可以使用Connection::getInstance('name')以不同的上下文(甚至不同的后端URL)让Connection类持有命名实例。例如

$admin_connection = Connection::getInstance('admin', array('auth' => $auth_token));

... 在代码的其他地方,你可以通过以下方式获取连接

$admin_connection = Connection::getInstance('admin');

安装

首选安装方式是通过 Composer

只需在项目的composer.json中添加

"require": {
  "textalk/webshop-client": "0.3.*"
}

变更日志

0.3.0

  • 添加对php ^7.2和php ^8.0版本的支持
  • 放弃对旧php版本的支持

0.2.6

  • 添加对http(s)连接的支持(默认为wss)

0.2.1

  • 使用tivoka 3.4.*,避免依赖的稳定性开发级别。

0.2.0

  • 显式始终使用WebSocket连接。
  • 添加设置连接选项的可能性,如头和超时。
  • 移除类名魔法中的大小写混淆;使用正确的大小写!