This repository was archived by the owner on Jan 15, 2019. It is now read-only.
forked from sajadbahar/customerio.php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerIO.php
More file actions
177 lines (149 loc) · 4.96 KB
/
CustomerIO.php
File metadata and controls
177 lines (149 loc) · 4.96 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
170
171
172
173
174
175
176
177
<?php
class CustomerIO {
var $errorMessage;
var $errorCode;
/**
* Cache the information on the API location on the server
*/
private $_apiUrl = 'https://track.customer.io/api/v1/customers/';
/**
* Default to a 300 second timeout on server calls
*/
private $_timeout = 300;
/**
* Default to a 8K chunk size
*/
var $chunkSize = 8192;
/**
* Cache the user api_key so we only have to log in once per client instantiation
*/
private $_apiKey;
/**
* Cache the user site_id so we only have to log in once per client instantiation
*/
private $_siteId;
/**
* Cache the user api_key so we only have to log in once per client instantiation
*/
private $_secure = false;
/**
* Connect to the Customer.io server.
*
* @param string $apikey Your Customer.io api key
* @param string $siteId Your Customer.io site id
* @param string $secure Whether or not this should use a secure connection
*/
function __construct($apikey, $siteId, $secure=false) {
$this->secure = $secure;
$this->_apiKey = $apikey;
$this->_siteId = $siteId;
}
function setTimeout($seconds){
if (is_int($seconds)){
$this->timeout = $seconds;
return true;
}
}
function getTimeout(){
return $this->timeout;
}
function useSecure($val){
if ($val===true){
$this->_secure = true;
} else {
$this->_secure = false;
}
}
/**
* Add new user to your account.
*
* @param integer $id User id in your system
* @param string $email User email address
* @param datetime $createTime User create time in your system
* @param array $extraInfo User extra info, like first_name, plan_name
*/
function addUser($id, $email, $createTime, $extraInfo=array()) {
$url = $this->_apiUrl . $id;
if (!is_array($extraInfo)) {
throw new Exception('$extraInfo param must be an array');
}
$extraInfo['email'] = $email;
$extraInfo['created_at'] = $createTime;
return $this->_callServer($url, $extraInfo, "PUT");
}
/**
* Edit user data.
*
* @param integer $id User id in your system
* @param array $extraInfo User extra info, like first_name, plan_name
*/
function EditUser($id, $extraInfo=array()) {
$url = $this->_apiUrl . $id;
if (!is_array($extraInfo)) {
throw new Exception('$extraInfo param must be an array');
}
return $this->_callServer($url, $extraInfo, "PUT");
}
/**
* Trigger event.
*
* @param integer $id User id in your system
* @param string $name Event name
* @param array $extraInfo event extra info
* @param datetime $timestamp if event happend in the past
*/
function triggerEvent($id, $name, $extraInfo = array(), $timestamp = null) {
$url = $this->_apiUrl . $id . '/events';
if (!isset($name)) {
throw new Exception('Event name can\t be null');
}
if (!is_array($extraInfo)) {
throw new Exception('$extraInfo param must be an array');
}
if(isset($extraInfo['name'])) {
throw new Exception('$extraInfo param must not contain the key name');
}
$data = array('name' => $name);
if(count($extraInfo) > 0) {
$data['data'] = $extraInfo;
}
if($timestamp != null) {
$data['timestamp'] = $timestamp;
}
return $this->_callServer($url, $data);
}
/**
* Call customer.io server.
*
* @param string $url Customer.io endpoint
* @param array $data User info
*/
private function _callServer($url, $data, $requestType="POST") {
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, false);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, $requestType);
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($session,CURLOPT_USERPWD, $this->_siteId . ":" . $this->_apiKey);
if (!$this->_secure) {
curl_setopt($session,CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($session, CURLOPT_SSLVERSION, 3);
curl_exec($session);
if(curl_errno($session))
{
$this->errorCode = curl_errno($session);
$this->errorMessage = curl_error($session);
curl_close($session);
return false;
} else {
curl_close($session);
return true;
}
}
}
?>