PHP code example of webmasterskaya / php-unisender-api

1. Go to this page and download the library: Download webmasterskaya/php-unisender-api 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/ */

    

webmasterskaya / php-unisender-api example snippets




ebmasterskaya\Unisender\Client;

$unisender = new Client((string) getenv('UNISENDER_API_KEY'));

$response = $unisender->send('createList', [
    'title' => 'Новости компании',
]);

$listId = $response['result']['id'];

$response = $unisender->getFields();
$response = $unisender->subscribe([
    'list_ids' => '12345',
    'fields' => [
        'email' => '[email protected]',
        'Name' => 'Иван',
    ],
    'double_optin' => 3,
]);

$name = htmlspecialchars($userName, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');

$response = $unisender->sendEmail([
    'email' => '[email protected]',
    'sender_name' => 'Company',
    'sender_email' => '[email protected]',
    'subject' => 'Тема письма',
    'body' => "<p>Привет, <strong>{$name}</strong>!</p>",
    'list_id' => 123,
]);

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Log\LoggerInterface;

$unisender = new Client(
    api_key: (string) getenv('UNISENDER_API_KEY'),
    options: ['lang' => 'en'],
    httpClient: $httpClient,
    requestFactory: $requestFactory,
    streamFactory: $streamFactory,
    logger: $logger,
);

use Webmasterskaya\Unisender\Exception\ApiException;
use Webmasterskaya\Unisender\Exception\NetworkException;

try {
    $response = $unisender->createList(['title' => 'Новости']);
} catch (ApiException $exception) {
    echo $exception->getApiCode();
    echo $exception->getStatusCode();
    echo $exception->getResponseBody();
} catch (NetworkException $exception) {
    // Ошибка PSR-18 транспорта: таймаут, DNS, TLS и т. п.
}
bash
composer