PHP code example of antonowano / mindbox

1. Go to this page and download the library: Download antonowano/mindbox library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antonowano / mindbox example snippets


$data = $apiClient->sync(new DefaultJsonOperation(
    name: '{prefix}GetProfile',
    needDeviceUuid: false,
    requestData: [
        'customer' => [
            'ids' => [
                'webID' => 3165145,
            ],
        ],
    ],
));

$data = $apiClient->async(new DefaultJsonOperation(
    name: '{prefix}ViewProduct',
    needDeviceUuid: true,
    requestData: [
        'viewProduct' => [
            'productGroup' => [
                'ids' => [
                    'brandProducts' => 3914381,
                ],
            ],
        ],
    ],
));

$csvFile = tmpfile();
fputcsv($csvFile, [
    'ExternalIdentityWebID',
    'IsSubscribedByEmail',
    'IsSubscribedByEmailOnService',
    'IsSubscribedByEmailOnTrigger',
    'IsSubscribedByEmailOnLoyalty',
], ';', '"', '\\');
fputcsv($csvFile, [
    '3165145',
    '0',
    '1',
    '1',
    '1',
], ';', '"', '\\');
rewind($csvFile);

$data = $apiClient->bulk(
    new DefaultBulkOperation(
        name: 'DirectCrm.Customers.Edit',
        resource: $csvFile,
    ),
    new MindboxTransactionId()
);

use Antonowano\Mindbox\Integration;

readonly class CustomIntegration implements Integration
{
    public function endpointId(): string
    {
        return env('MINDBOX_ENDPOINT_ID');
    }

    public function secretKey(): string
    {
        return env('MINDBOX_SECRET_KEY');
    }

    public function defaultPrefix(): string
    {
        return env('MINDBOX_DEFAULT_PREFIX');
    }

    public function deviceUuid(): ?string
    {
        return $_COOKIE['mindboxDeviceUUID'] ?? null;
    }
}

use Antonowano\Mindbox\AsyncOperation;

readonly class ViewProduct implements AsyncOperation
{
    public function __construct(
        private int $productId,
    ) {
    }

    public function name(string $defaultPrefix): string
    {
        return $defaultPrefix . 'ViewProduct';
    }

    public function needDeviceUuid(): bool
    {
        return true;
    }

    public function requestData(): array
    {
        return [
            'viewProduct' => [
                'productGroup' => [
                    'ids' => [
                        'brandProducts' => $this->productId,
                    ],
                ],
            ],
        ];
    }
}

$data = $apiClient->async(new ViewProduct($productId));

'mindbox' => [
    'endpoint_id' => env('MINDBOX_ENDPOINT_ID'),
    'secret_key' => env('MINDBOX_SECRET_KEY'),
    'default_prefix' => env('MINDBOX_DEFAULT_PREFIX'),
],

use Antonowano\Mindbox\ApiClient;
use Antonowano\Mindbox\DefaultIntegration;
use Antonowano\Mindbox\Integration;
use Antonowano\Mindbox\MindboxApiClient;
use Antonowano\Mindbox\MindboxEndpointUrl;
use Antonowano\Mindbox\SilentApiClient;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Illuminate\Support\Facades\Log;

public function register(): void
{
    $this->app->singleton(Integration::class, function () {
        return new DefaultIntegration(
            endpointId: config('services.mindbox.endpoint_id'),
            secretKey: config('services.mindbox.secret_key'),
            defaultPrefix: config('services.mindbox.default_prefix'),
            deviceUuid: $_COOKIE['mindboxDeviceUUID'] ?? null,
        );
    });

    $this->app->singleton(ApiClient::class, function ($app) {
        $integration = $app->make(Integration::class);
        $httpFactory = new HttpFactory();

        $client = new MindboxApiClient(
            integration: $integration,
            endpointUrl: new MindboxEndpointUrl($integration),
            httpClient: new Client(),
            requestFactory: $httpFactory,
            streamFactory: $httpFactory,
        );

        return $client;
        // или, если никогда не хотите обрабатывать Exception, тогда так:
        // return new SilentApiClient($client, Log::channel());
    });
}

public function product(int $productId, ApiClient $client) {
    $client = new SilentApiClient($client, Log::channel());
    $client->async(new ViewProduct($productId));
}
bash
php artisan config:cache