Repository navigation
Expand file tree
/
Copy pathPepoCampaigns.class.php
More file actions
169 lines (160 loc) · 6.3 KB
/
Copy pathPepoCampaigns.class.php
File metadata and controls
169 lines (160 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
class PepoCampaigns
{
private $pepo_key;
private $pepo_secret;
private $pepo_url;
public function __construct($keys){
$this->pepo_key = $keys['key'];
$this->pepo_secret = $keys['secret'];
$this->pepo_url = 'https://pepocampaigns.com';
}
/**
* Creating a new list
* @link https://know.pepocampaigns.com/articles/managing-lists-api/#creating-a-new-list
*
* @param array $data Associative array with 'name' and 'source' keys
* @return string Raw response from API
*/
public function create_list($data){
$api_url = '/api/v1/list/create/';
$request_time = $this->get_request_time();
$delimiter = '::';
$signature = $this->generate_signature($this->pepo_secret, $api_url.$delimiter.$request_time);
$fields = array(
'request-time' => $request_time,
'signature' => $signature,
'api-key' => $this->pepo_key,
'name' => $data['name'],
'source' => $data['source']
);
$result = $this->execute_curl($api_url, $fields);
return $result;
}
/**
* Adding a contact to a List
* @link https://know.pepocampaigns.com/articles/managing-lists-api/#adding-a-contact-to-a-list
*
* @param int $list_id List ID to add the contact
* @param array $data Associative array with 'email','first_name' and 'last_name' keys
* @return string Raw response from API
*/
public function add_contact_to_list($list_id, $data){
$api_url = '/api/v1/list/'.$list_id.'/add-contact/';
$request_time = $this->get_request_time();
$delimiter = '::';
$signature = $this->generate_signature($this->pepo_secret, $api_url.$delimiter.$request_time);
$fields = array(
'request-time' => $request_time,
'signature' => $signature,
'api-key' => $this->pepo_key,
'email' => $data['email'],
'attributes[First Name]' => $data['first_name'],
'attributes[Last Name]' => $data['last_name']
);
$result = $this->execute_curl($api_url, $fields);
return $result;
}
/**
* Removing a contact from a list
* @link https://know.pepocampaigns.com/articles/managing-lists-api/#removing-a-contact-from-a-list
*
* @param int $list_id List ID to add the contact
* @param array $data Associative array with 'email' key
* @return string Raw response from API
*/
public function remove_contact_from_list($list_id, $data){
$api_url = '/api/v1/list/'.$list_id.'/remove-contact/';
$request_time = $this->get_request_time();
$delimiter = '::';
$signature = $this->generate_signature($this->pepo_secret, $api_url.$delimiter.$request_time);
$fields = array(
'request-time' => $request_time,
'signature' => $signature,
'api-key' => $this->pepo_key,
'email' => $data['email']
);
$result = $this->execute_curl($api_url, $fields);
return $result;
}
/**
* Changing User Status
* @link https://know.pepocampaigns.com/articles/managing-contacts-api/#changing-user-status
*
* @param array $data Associative array with 'email' and 'type' key
* @return string Raw response from API
*/
public function change_user_status($list_id, $data){
$api_url = '/api/v1/list/'.$list_id.'/change-status/';
$request_time = $this->get_request_time();
$delimiter = '::';
$signature = $this->generate_signature($this->pepo_secret, $api_url.$delimiter.$request_time);
$fields = array(
'request-time' => $request_time,
'signature' => $signature,
'api-key' => $this->pepo_key,
'email' => $data['email'],
'type' => $data['type']
);
$result = $this->execute_curl($api_url, $fields);
return $result;
}
/**
* Send Transactional Email
* @link https://know.pepocampaigns.com/articles/managing-transactional/#send-transactional-email
*
* @param string $email Email
* @param string $template Template
* @param array $email_vars Associative array of email variables
* @return string Raw response from API
*/
public function send_transactional_email($email, $template, $email_vars){
$api_url = '/api/v1/send/';
$request_time = $this->get_request_time();
$delimiter = '::';
$signature = $this->generate_signature($this->pepo_secret, $api_url.$delimiter.$request_time);
$fields = array(
'request-time' => $request_time,
'signature' => $signature,
'api-key' => $this->pepo_key,
'email' => $email,
'template' => $template,
'email_vars' => json_encode($email_vars)
);
$result = $this->execute_curl($api_url, $fields);
return $result;
}
private function execute_curl($api_url, $fields){
$fields_string = http_build_query($fields);
try {
$ch = curl_init();
$url= $this->pepo_url.$api_url;
$curl_options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSLVERSION => 6,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields_string,
);
curl_setopt_array($ch, $curl_options);
$result = curl_exec($ch);
if ($result === FALSE)
throw new Exception(curl_error($ch), curl_errno($ch));
} catch (Exception $e){
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
curl_close( $ch );
return $result;
}
private function generate_signature($api_secret, $signature){
return hash_hmac('sha256', $signature, $api_secret, false);
}
private function get_request_time(){
return date("Y-m-d\TH:i:sP");
}
}