PHP code example of sendpulse / rest-api

1. Go to this page and download the library: Download sendpulse/rest-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/ */

    

sendpulse / rest-api example snippets


use Sendpulse\RestApi\Client;

$client = new Client(apiKey: 'YOUR_API_KEY');

$client = new Client(
    clientId:     'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
);

$email = $client->emailService();

// Campaigns
$campaigns = $email->campaigns()->getCampaigns(limit: 50);  // CampaignSummary[]
$campaign  = $email->campaigns()->getCampaignById(123);     // CampaignDetails

// Mailing lists
$lists = $email->mailingLists()->getMailingLists();         // array
$list  = $email->mailingLists()->getMailingListById(456);   // array

$email->mailingLists()->createMailingList(['name' => 'My list']);
$email->mailingLists()->updateMailingList(456, ['name' => 'Renamed']);
$email->mailingLists()->deleteMailingList(456);

$smtp = $client->smtpService();

$emails = $smtp->emails()->getSmtpEmails(limit: 100, offset: 0);  // EmailRecord[]
$email  = $smtp->emails()->getSmtpEmailInfo('message-id');         // EmailRecord

$smtp->emails()->sendSmtpEmail([
    'email' => [
        'html'    => '<h1>Hello</h1>',
        'text'    => 'Hello',
        'subject' => 'Test',
        'from'    => ['name' => 'Sender', 'email' => '[email protected]'],
        'to'      => [['name' => 'Recipient', 'email' => '[email protected]']],
    ],
]);

$sms = $client->smsService();

$campaigns = $sms->campaigns()->getSmsCampaigns();    // array
$campaign  = $sms->campaigns()->getSmsCampaignInfo(789); // array

$crm = $client->crmService();

// Contacts
$contacts = $crm->contacts()->getContactsList();                                    // Contact[]
$contacts = $crm->contacts()->getContactListByEmail(['email' => '[email protected]']); // with filter
$contact  = $crm->contacts()->getContactById(1);                                   // array

// Deals
$deals = $crm->deals()->getDealsList();                          // array
$deals = $crm->deals()->getDealsList(['pipeline_id' => 5]);     // with filter
$deal  = $crm->deals()->getDeal(10);                            // array

$bots = $client->chatbotService()->bots()->getBots();  // array

use Sendpulse\RestApi\Exception\AuthException;
use Sendpulse\RestApi\Exception\ForbiddenException;
use Sendpulse\RestApi\Exception\RateLimitException;
use Sendpulse\RestApi\Exception\ApiException;
use Sendpulse\RestApi\Exception\NetworkException;
use Sendpulse\RestApi\Exception\ProtocolException;

try {
    $campaigns = $client->emailService()->campaigns()->getCampaigns();
} catch (AuthException $e) {
    // 401 — invalid or expired credentials
} catch (ForbiddenException $e) {
    // 403 — tariff or permission restriction
} catch (RateLimitException $e) {
    // 429 — back off and retry
} catch (ApiException $e) {
    echo $e->httpStatus; // other 4xx / 5xx
    echo $e->rawBody;
} catch (ProtocolException $e) {
    // response received but could not be parsed (malformed JSON from API)
} catch (NetworkException $e) {
    // curl / transport error
}

$client = new Client(
    apiKey:         'key',
    connectTimeout: 10,   // seconds, default
    requestTimeout: 30,   // seconds, default
);

use Sendpulse\RestApi\Http\Adapter\Psr18Adapter;

$client = new Client(
    apiKey:     'key',
    httpClient: new Psr18Adapter(
        client:         $yourPsr18Client,
        requestFactory: $requestFactory,
        streamFactory:  $streamFactory,
    ),
);

use Sendpulse\RestApi\Token\Psr16TokenStorage;
use Sendpulse\RestApi\Token\InMemoryTokenStorage;

// PSR-16 cache (e.g. Symfony Cache, Laravel Cache)
$storage = new Psr16TokenStorage($psr16Cache);

// In-memory (tokens lost on process exit)
$storage = new InMemoryTokenStorage();

$client = new Client(
    clientId:     'id',
    clientSecret: 'secret',
    tokenStorage: $storage,
);