-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendloopengageapi.php
More file actions
120 lines (100 loc) · 3.21 KB
/
Copy pathsendloopengageapi.php
File metadata and controls
120 lines (100 loc) · 3.21 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
<?php
/*
Sendloop Engage API PHP Library
(c)Copyright Sendloop. All rights reserved.
This API library helps you to make connections to Sendloop Engage API
quickly without digging into curl settings.
$api = new SendloopEngageAPI("your_sendloop_api_key_here");
// To track a person
$api->trackPerson("user02942", "test@test.com", array('name' => 'test', 'age' => 24, 'birth_date' => '1977-12-10 23:20:00'));
// To get person profile
$api->getPerson("user02942");
// To delete person profile
$api->deletePerson("user02942");
*/
class SendloopEngageAPI {
protected $_errorMessage;
protected $_apiKey;
/**
* Constructor
* @param string $apiKey Your Sendloop Engage API key
*/
public function __construct($apiKey)
{
$this->_apiKey = $apiKey;
}
/**
* Get person data
* @param integer $userId Person identifier
* @return An object containing person information
*/
public function getPerson($userId)
{
$response = $this->_talkWithServer('person.json', 'get', array('user_id' => $userId));
if (isset($response->Person)) return $response->Person;
return false;
}
/**
* Track person data
* @param integer $userId Person identifier
* @param string $email Person email address
* @param array $params An associative array of person information to be tracked
* @return bool
*/
public function trackPerson($userId, $email, $params = array())
{
$params['user_id'] = $userId;
$params['email'] = $email;
$response = $this->_talkWithServer('person.json', 'post', $params);
if (isset($response->Status) && $response->Status == 'Okay') return true;
return false;
}
/**
* Delete person
* @param integer $userId Person identifier
* @return bool
*/
public function deletePerson($userId)
{
$params['user_id'] = $userId;
$response = $this->_talkWithServer('person.json', 'delete', $params);
if (isset($response->Status) && $response->Status == 'Okay') return true;
return false;
}
/**
* Returns API error message
* @return string
*/
public function getError()
{
return $this->_errorMessage;
}
protected function _talkWithServer($resource, $httpMethod = 'get', $params = array())
{
$httpMethod = strtoupper($httpMethod);
if (! in_array($httpMethod, array('GET', 'DELETE', 'POST'))) throw new Exception('Invalid HTTP method: '.$httpMethod);
$this->_errorMessage = '';
$data = http_build_query($params);
$requestHeaders = array();
$requestHeaders[] = "Authorization: Basic " . base64_encode("{$this->_apiKey}:");
if ($httpMethod == 'POST') {
$requestHeaders[] = 'Content-type: application/x-www-form-urlencoded';
}
$contextOptions = array();
$contextOptions['header'] = $requestHeaders;
$contextOptions['method'] = $httpMethod;
$contextOptions['ignore_errors'] = true;
if ($httpMethod == 'POST') {
$contextOptions['content'] = $data;
}
$context = stream_context_create(array('http' => $contextOptions));
$url = 'http://sendloop.com/api/v4/'.$resource.'/'.($httpMethod == 'GET' || $httpMethod == 'DELETE' ? '?'.$data : '');
$response = @file_get_contents($url, false, $context);
$response = json_decode($response);
if ($response->Status !== 'Okay') {
$this->_errorMessage = $response->Status;
return false;
}
return $response;
}
}