conesso/client

用PHP编写的用于 https://www.conesso.io/ 的API客户端。

0.1.2 2023-11-13 09:42 UTC

This package is auto-updated.

Last update: 2024-09-13 17:40:41 UTC


README

Conesso header

入门指南

composer require conesso/client

此库旨在使用任何已集成到您的项目中的PSR-18客户端。请确保允许php-http/discovery composer插件运行或手动安装您选择的客户端。

Guzzle是一个知名且广泛使用的PSR-18 HTTP客户端包。

composer require guzzlehttp/guzzle

创建您的配置API客户端。

use Conesso\Conesso;

$apiKey = '9gba262882g87f3b31e4f843adf3d66f19d322d6d7673b19c3e61f6f07abf2a5';

$client = Conesso::client($apiKey);

如果您需要更多控制HTTP客户端,可以创建并配置自己的。

<?php

$apiKey = '9gba262882g87f3b31e4f843adf3d66f19d322d6d7673b19c3e61f6f07abf2a5';

$httpClient = new GuzzleHttp\Client();

$client = Conesso::factory()
    ->withApiKey($apiKey)
    ->withHttpHeader('User-Agent', 'MyApp/1.0')
    ->withBaseUri('https://sandbox.conesso.io')
    ->withHttpClient($httpClient)
    ->make();

用法

购物车资源 /v2/carts(📕 API文档

列出购物车

<?php

$carts = $conesso->carts()->list([
    'count' => 10,
    'page' => 1,
    'filter' => 'John',
    'searchKey' => 'customerFirstname',
]);

foreach ($carts as $cart) {
    echo $cart->id . PHP_EOL; // 1
    echo $cart->customerFirstname . PHP_EOL; // John
}

检索购物车

<?php

$cart = $conesso->carts()->retrieve('1f6ef9fcd71g732c61bf03d5fabc2034');

echo $cart->id . PHP_EOL; // 1f6ef9fcd71g732c61bf03d5fabc2034
echo $cart->customerEmail . PHP_EOL; // john.doe@example
echo $cart->customerFirstname . PHP_EOL; // John
echo $cart->customerLastname . PHP_EOL; // Doe

foreach ($cart->cartProducts as $product) {
    echo $product->sku . PHP_EOL; // 123456
    echo $product->name . PHP_EOL; // Product 1
}

创建购物车

<?php


$cartData = [
    'customerEmail' => 'john.doe@example.com',
    'apiReferenceId' => '123456789',
    'customerIsGuest'=> true,
    'cartProducts' => [
        [
            'name' => 'Product 1',
            'sku' => '123456789',
            'price' => 100,
            'quantity' => 1,
        ]
    ]
];

$cart = $conesso->carts()->create($cartData);

echo $cart->id . PHP_EOL; // 1f6ef9fcd71g732c61bf03d5fabc2034
echo $cart->createdAt; // 2020-10-30T09:00:00+00:00

更新购物车

$cartData = [
    'customerEmail' => 'john.doe+updated@example.com',
];

$cart = $conesso->carts()->update('1f6ef9fcd71g732c61bf03d5fabc2034', $cartData);

echo $cart->customEmail . PHP_EOL; // john.doe+updated@example.com

删除购物车

<?php

$deleted = $conesso->carts()->delete('1f6ef9fcd71g732c61bf03d5fabc2034');

echo $deleted->id; . PHP_EOL; // 1f6ef9fcd71g732c61bf03d5fabc2034
echo $deleted->deleted; . PHP_EOL; // true

联系自定义字段 /v2/custom-fields(📕 API文档

列出联系自定义字段

<?php

$customFields = $conesso->customFields()->list([
    'count' => 10,
    'page' => 1
]);

foreach ($customFields->data as $customField) {
    echo $customField->id . PHP_EOL; // 1
    echo $customField->name . PHP_EOL; // Custom Field 1
    echo $customField->isPrivate . PHP_EOL; // false
}

检索联系自定义字段

<?php

$customField = $conesso->customFields()->retrieve(1);

echo $customField->id . PHP_EOL; // 1
echo $customField->name . PHP_EOL; // Custom Field 1
echo $customField->isPrivate . PHP_EOL; // false

创建联系自定义字段

<?php

$customFieldData = [
    'name' => 'Custom Field 1',
    'dataType' => 'string',
    'nameKey' => 'custom_field_1',
    'description' => 'Custom Field 1 Description',
    'defaultValue' => 'Custom Field 1 Default Value',
    'isPrivate' => false,
    'createdAt' => '2023-06-20T09:14:15.000Z',
    'createdBy' => 'Test User',
    'updatedAt' => '2023-06-20T09:14:15.000Z',
    'updatedBy' => 'Test User',
];

$created = $conesso->customFields()->create($customFieldData);

echo $created->id . PHP_EOL; // 1
echo $created->name . PHP_EOL; // Custom Field 1

更新联系自定义字段

<?php

$customFieldData = [
    'name' => 'Custom Field 1 Updated',
];

$updated = $conesso->customFields()->update(1, $customFieldData);

echo $updated->id . PHP_EOL; // 1
echo $updated->name . PHP_EOL; // Custom Field 1 Updated

删除联系自定义字段

<?php

$deleted = $conesso->customFields()->delete(1);

echo $deleted->id . PHP_EOL; // 1
echo $deleted->deleted . PHP_EOL; // true