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



Sendpulse\RestApi\ApiClient;
use Sendpulse\RestApi\Storage\FileStorage;
use Sendpulse\RestApi\ApiClientException;

// API credentials from https://login.sendpulse.com/settings/#api
define('API_USER_ID', '');
define('API_SECRET', '');
define('PATH_TO_ATTACH_FILE', __FILE__);

$apiClient = new ApiClient(API_USER_ID, API_SECRET, new FileStorage());


/*
 * Send GET request
 * 
 * Example: Get a List of Mailing Lists
 */
try {
    $addressBooks = $apiClient->get('addressbooks', [
        'limit' => 100,
        'offset' => 0
    ]);

    var_dump($addressBooks);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}


/*
 * Send POST request 
 * 
 * Example: Add new email to mailing lists
 */
try {
    $addEmailsResult = $apiClient->post('addressbooks/33333/emails', [
        'emails' => [
            [
                'email' => '[email protected]',
                'variables' => [
                    'phone' => '+123456789',
                    'my_var' => 'my_var_value'
                ]
            ], [
                'email' => '[email protected]',
                'variables' => [
                    'phone' => '+987654321',
                    'my_var' => 'my_var_value'
                ]
            ]
        ]
    ]);

    var_dump($addEmailsResult);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}
 
 
/*
 * Send PUT request 
 * 
 * Example: Edit a Mailing List
 */
try {
    $addEmailsResult = $apiClient->put('addressbooks/33333', [
        'name' => "New Name"
    ]);

    var_dump($addEmailsResult);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}


/*
 * Send PATCH request 
 * 
 * Example: Edit Scheduled Campaign
 */
try {
    $editScheduledCampaignResult = $apiClient->patch('campaigns/333333', [
        "name" => "My_API_campaign",
        "sender_name" => "sender",
        "sender_email" => "[email protected]",
        "subject" => "Hello customer",
        "template_id" => 351594,
        "send_date" => "2023-10-21 11:45:00"
    ]);

    var_dump($editScheduledCampaignResult);
} catch (\Sendpulse\RestApi\ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}


/*
 * Send DELETE request 
 * 
 * Example: Delete Emails from a Mailing List
 */
try {
    $removeEmailsResult = $apiClient->delete('addressbooks/33333/emails', [
        'emails' => ['[email protected]']
    ]);

    var_dump($removeEmailsResult);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}


/*
 * Example: Start Automation360 event
 */
try {
    $startEventResult = $apiClient->post('events/name/my_event_name', [
        "email" => "[email protected]",
        "phone" => "+123456789",
        "products" => [
            [
                "id" => "id value",
                "name" => "name value"
            ]
        ]
    ]);

    var_dump($startEventResult);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}


/**
 * Example: Crm create a new deal
 */
try {
    $crmCreateDeal = $apiClient->post('crm/v1/deals', [
        "pipelineId" => 0,
        "stepId" => 0,
        "responsibleId" => 0,
        "name" => "string",
        "price" => 0,
        "currency" => "string",
        "sourceId" => 0,
        "contact" => [
            0
        ],
        "attributes" => [
            [
                "attributeId" => 0,
                "value" => "string"
            ]
        ],
        "attachments" => [
            "https://link-to-file.com/file.jpg"
        ]
    ]);

    var_dump($crmCreateDeal);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}


/**
 * Example: Whatsapp send a template message to the specified contact
 */
try {
    $sendTemplateByPhoneResult = $apiClient->post('whatsapp/contacts/sendTemplateByPhone', [
        "bot_id" => "xxxxxxxxxxxxxxxxxxxxxxxx",
        "phone" => "380931112233",
        "template" => [
            "name" => "thanks_for_buying",
            "language" => [
                "code" => "en"
            ],
            "components" => []
        ]
    ]);

    var_dump($sendTemplateByPhoneResult);
} catch (ApiClientException $e) {
    var_dump([
        'message' => $e->getMessage(),
        'http_code' => $e->getCode(),
        'response' => $e->getResponse(),
        'curl_errors' => $e->getCurlErrors(),
        'headers' => $e->getHeaders()
    ]);
}