Skip to content

Commit

Permalink
Merge pull request #81 from sendpulse/new_major_vesion
Browse files Browse the repository at this point in the history
New major vesion
  • Loading branch information
bilovol authored Oct 11, 2023
2 parents 1872505 + 64d152b commit e1a81e2
Show file tree
Hide file tree
Showing 13 changed files with 1,392 additions and 1,798 deletions.
251 changes: 160 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
# SendPulse REST client library
SendPulse REST client library
================

[![License](http://poser.pugx.org/sendpulse/rest-api/license)](https://packagist.org/packages/sendpulse/rest-api)
[![Total Downloads](http://poser.pugx.org/sendpulse/rest-api/downloads)](https://packagist.org/packages/sendpulse/rest-api)
[![PHP Version Require](http://poser.pugx.org/sendpulse/rest-api/require/php)](https://packagist.org/packages/sendpulse/rest-api)

A simple SendPulse REST client library and example for PHP.

API Documentation [https://sendpulse.com/api](https://sendpulse.com/api)

### Installing

### Requirements

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


### Installation

Via Composer:

```bash
composer require sendpulse/rest-api
```

### Usage
### Example

```php
<?php
Expand All @@ -29,114 +42,170 @@ 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__);

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


/*
* Example: Get Mailing Lists
* Send GET request
*
* Example: Get a List of Mailing Lists
*/
var_dump($SPApiClient->listAddressBooks());
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
*/
$bookID = 123;
$emails = array(
array(
'email' => '[email protected]',
'variables' => array(
'phone' => '+12345678900',
'name' => 'User Name',
)
)
);
$additionalParams = array(
'confirmation' => 'force',
'sender_email' => '[email protected]',
);
// With confirmation
var_dump($SPApiClient->addEmails($bookID, $emails, $additionalParams));

// Without confirmation
var_dump($SPApiClient->addEmails($bookID, $emails));

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


/*
* Example: Send mail using SMTP
* Send PUT request
*
* Example: Edit a Mailing List
*/
$email = array(
'html' => '<p>Hello!</p>',
'text' => 'Hello!',
'subject' => 'Mail subject',
'from' => array(
'name' => 'John',
'email' => '[email protected]',
),
'to' => array(
array(
'name' => 'Subscriber Name',
'email' => '[email protected]',
),
),
'bcc' => array(
array(
'name' => 'Manager',
'email' => '[email protected]',
),
),
'attachments' => array(
'file.txt' => file_get_contents(PATH_TO_ATTACH_FILE),
),
);
var_dump($SPApiClient->smtpSendMail($email));
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()
]);
}


/*
* Example: create new push
* Send PATCH request
*
* Example: Edit Scheduled Campaign
*/
$task = array(
'title' => 'Hello!',
'body' => 'This is my first push message',
'website_id' => 1,
'ttl' => 20,
'stretch_time' => 0,
);

// This is optional
$additionalParams = array(
'link' => 'http://yoursite.com',
'filter_browsers' => 'Chrome,Safari',
'filter_lang' => 'en',
'filter' => '{"variable_name":"some","operator":"or","conditions":[{"condition":"likewith","value":"a"},{"condition":"notequal","value":"b"}]}',
);
var_dump($SPApiClient->createPushTask($task, $additionalParams));
```
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()
]);
}

### Usage Automation360

```php
<?php
/*
* 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()
]);
}

require 'vendor/autoload.php';
/*
* 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()
]);
}

use Sendpulse\RestApi\Automation360;

// https://login.sendpulse.com/emailservice/events/
$eventHash = 'EVENT_HASH';
$email = '[email protected]';
$phone = '380931112233';
$variables = [
'user_id' => 123123,
'event_date' => date('Y-m-d'),
'firstname' => 'Name',
'lastname' => 'Family',
'age' => 23
];
$automationClient = new Automation360($eventHash);
$result = $automationClient->sendEventToSendpulse($email, $phone, $variables);

var_dump($result);
```

8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
{
"name": "Alexey Moroz",
"email": "[email protected]"
},
{
"name": "Evgeniy Bilovol",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0"
"php": ">=7.1.0",
"ext-json": "*",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit e1a81e2

Please sign in to comment.