Giter Site home page Giter Site logo

sendpulse-rest-api-php's Introduction

SendPulse REST client library

License Total Downloads PHP Version Require

A simple SendPulse REST client library and example for PHP.

API Documentation https://sendpulse.com/api

Requirements

  • php: >=7.1.0
  • ext-json: *
  • ext-curl: *

Installation

Via Composer:

composer require sendpulse/rest-api

Example

<?php
require 'vendor/autoload.php';

use 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()
    ]);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.