Skip to content

Commit 66696c7

Browse files
author
bilovol
committed
add Automation360
1 parent 5b9b25f commit 66696c7

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# SendPulse REST client library
2+
23
A simple SendPulse REST client library and example for PHP.
34

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

67
### Installing
8+
79
Via Composer:
10+
811
```bash
912
composer require sendpulse/rest-api
1013
```
1114

1215
### Usage
16+
1317
```php
1418
<?php
1519
require 'vendor/autoload.php';
@@ -111,3 +115,28 @@ $additionalParams = array(
111115
var_dump($SPApiClient->createPushTask($task, $additionalParams));
112116
```
113117

118+
### Usage Automation360
119+
120+
```php
121+
<?php
122+
123+
require 'vendor/autoload.php';
124+
125+
use Sendpulse\RestApi\Automation360;
126+
127+
// https://login.sendpulse.com/emailservice/events/
128+
$eventHash = 'EVENT_HASH';
129+
$email = '[email protected]';
130+
$phone = '380931112233';
131+
$variables = [
132+
'user_id' => 123123,
133+
'event_date' => date('Y-m-d'),
134+
'firstname' => 'Name',
135+
'lastname' => 'Family',
136+
'age' => 23
137+
];
138+
$automationClient = new Automation360($eventHash);
139+
$result = $automationClient->sendEventToSendpulse($email, $phone, $variables);
140+
141+
var_dump($result);
142+
```

example/index.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Sendpulse\RestApi\ApiClient;
1515
use Sendpulse\RestApi\Storage\FileStorage;
16+
use Sendpulse\RestApi\Automation360;
1617

1718
define('API_USER_ID', '');
1819
define('API_SECRET', '');
@@ -163,4 +164,22 @@
163164
var_dump($SPApiClient->getSmsCampaignCost($params));
164165

165166
// Delete sms campaign
166-
var_dump($SPApiClient->deleteSmsCampaign(CAMPAIGN_ID));
167+
var_dump($SPApiClient->deleteSmsCampaign(CAMPAIGN_ID));
168+
169+
170+
// Send event
171+
$eventHash = 'EVENT_HASH';
172+
$email = '[email protected]';
173+
$phone = '380931112233';
174+
$variables = [
175+
'user_id' => 123123,
176+
'event_date' => date('Y-m-d'),
177+
'firstname' => 'Name',
178+
'lastname' => 'Family',
179+
'age' => 23
180+
];
181+
182+
$automationClient = new Automation360($eventHash);
183+
$result = $automationClient->sendEventToSendpulse($email, $phone, $variables);
184+
185+
var_dump($result);

src/Automation360.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Sendpulse\RestApi;
4+
5+
use Exception;
6+
7+
class Automation360
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $eventHash;
13+
/**
14+
* @var string
15+
*/
16+
private $eventDomain = 'https://events.sendpulse.com/events/id/';
17+
18+
/**
19+
* Automation360 constructor.
20+
* @param string $eventHash
21+
* @throws Exception
22+
*/
23+
public function __construct($eventHash)
24+
{
25+
if (!$eventHash) {
26+
throw new Exception('Invalid parameter $eventHash', 500);
27+
}
28+
$this->eventHash = $eventHash;
29+
}
30+
31+
/**
32+
* One of the variables $email or $phone
33+
* must necessarily contain the correct value
34+
*
35+
* @param null|string $email
36+
* @param null|string $phone
37+
* @param array $variables
38+
* @return array
39+
* @throws Exception
40+
*/
41+
public function sendEventToSendpulse($email = null, $phone = null, array $variables = [])
42+
{
43+
if (!$email && !$phone) {
44+
throw new \Exception('Variables $email and $phone is empty', 500);
45+
}
46+
if ($email) {
47+
$variables['email'] = $email;
48+
}
49+
if ($phone) {
50+
$variables['phone'] = $phone;
51+
}
52+
return $this->sendRequest($variables);
53+
}
54+
55+
/**
56+
* Send request to SendPulse with CURL
57+
*
58+
* @param array $variables
59+
* @return array
60+
*/
61+
private function sendRequest(array $variables)
62+
{
63+
$url = $this->eventDomain . $this->eventHash;
64+
65+
$curl = curl_init();
66+
curl_setopt($curl, CURLOPT_POST, count($variables));
67+
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($variables));
68+
curl_setopt($curl, CURLOPT_URL, $url);
69+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
70+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
71+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
72+
curl_setopt($curl, CURLOPT_HEADER, true);
73+
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
74+
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
75+
$response = curl_exec($curl);
76+
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
77+
$headerCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
78+
$responseBody = substr($response, $header_size);
79+
curl_close($curl);
80+
81+
$result = [
82+
'http_code' => $headerCode,
83+
'data' => json_decode($responseBody, true)
84+
];
85+
86+
return $result;
87+
}
88+
}

0 commit comments

Comments
 (0)