-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPardotConnector.class.php
217 lines (202 loc) · 7.22 KB
/
PardotConnector.class.php
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* This is a basic class for connecting to the Pardot API
* The important parts here are the authenticate, send, and various prospect functions
* Check out Prospect.class.php as a way to manipulate prospects.
*
* @author stephenreid
*
* @since 7/10/2012
* @desc A connecting class to the pardot api
*
* @method simpleXmlObject accountRead() Read account information
* @method simpleXmlObject create($objectType,$object)
* @method array query($objectType,$queryParameters) Runs the query action against an object type
* @method simpleXmlObject read($objectType, $identifierAssociativeArray)
* @method simpleXmlObject update($objectType, $object)
* @method simpleXmlObject upsert($objectType, $object)
* @method object campaignCreate($object)
* @method object campaignQuery($criteria)
* @method object campaignRead($criteria)
* @method object campaignUpdate($object)
* @method object customFieldQuery($criteria)
* @method object customFieldRead($criteria)
* @method object customRedirectQuery($criteria)
* @method object customRedirectRead($criteria)
* @method object emailRead($criteria)
* @method object emailSend($object)
* @method object visitorQuery($criteria)
* @method object visitorRead($criteria)
* @method object formQuery($criteria)
* @method object formRead($criteria)
* @method object listQuery($criteria)
* @method object listRead($criteria)
* @method object opportunityQuery($criteria)
* @method object opportunityRead($criteria)
* @method object opportunityUpdate($criteria)
* @method object prospectCreate($object)
* @method object prospectQuery($criteria)
* @method object prospectRead($criteria)
* @method object prospectUpdate($object)
* @method object prospectUpsert($object)
* @method object userQuery($criteria)
* @method object userRead($criteria)
* @method object visitorActivityQuery($criteria)
* @method object visitorActivityRead($criteria)
* @method object visitQuery($criteria)
* @method object visitRead($criteria)
*/
class PardotConnector
{
//A flag for echoing debug output
private $debug = false;
private $apiKey = null;
private $outputMode = 'simple'; // choose between 'simple','full','mobile'
/** It's Best if you set Authentication Through Your Server Environment Vars not Files **/
private $email = '';
private $password = '';
private $userKey = '';
private $objectTypes = array(
'account',
'campaign',
'customField',
'customRedirect',
'dynamicContent',
'email',
'form',
'list',
'listMembership',
'opportunity',
'opportunityProspect',
'prospect',
'user',
'visitorActivity',
'visitor',
);
/**
* __construct PardotConnector()
* Dummy Constructor, Run authenticate() to be able to do anything.
*/
public function __construct()
{
}
public function __call($name, $args = array())
{
if ($name === 'create') {
return $this->sendRequest($args['0'], $name, $args['1'])->$args['0'];
} elseif ($name === 'query') {
return $this->baseQuery($args['0'], $args['1']);
} elseif ($name === 'read') {
return $this->sendRequest($args['0'], $name, $args['1'])->$args['0'];
} elseif ($name === 'update') {
return $this->sendRequest($args['0'], $name, $args['1'])->$args['0'];
} elseif ($name === 'upsert') {
return $this->sendRequest($args['0'], $name, $args['1'])->$args['0'];
} elseif ($name === 'send') {
return $this->sendRequest($args['0'], $name, $args['1'])->$args['0'];
}
//Reverse sort this, so we have longest words first
sort($this->objectTypes);
$objectTypes = array_reverse($this->objectTypes);
foreach ($objectTypes as $type) {
if (strpos($name, $type) === 0) {
$action = strtolower(str_replace($type, '', $name));
if (count($args) === 0) {
return $this->$action($type, $args);
} else {
return $this->$action($type, $args[0]);
}
}
}
return false;
}
/**
* Must call this function before making any other API calls.
*
* @param string|null $username
* @param string|null $password
* @param string|null $userKey
*
* @return SimpleXMLElement
*
* @throws PardotConnectorException
*/
public function authenticate($username = null, $password = null, $userKey = null)
{
//gets a user api key back
if ($username != null) {
$params = array('email' => $username,'password' => $password,'user_key' => $userKey);
$this->userKey = $userKey;
} else {
$params = array('email' => $this->email,'password' => $this->password,'user_key' => $this->userKey);
}
$ret = $this->sendRequest('login', '', $params);
$this->apiKey = $ret->api_key;//add error handling to this later
return $ret;
}
/**
* baseQuery.
*
* @param $objectType (The type of object we're querying)
* @param $queryParams (array of query critiera)
*
* @return SimpleXMLElement
*/
private function baseQuery($objectType, $queryParams)
{
$objects = $this->sendRequest($objectType, 'query', $queryParams)->result;
return $objects;
}
/**
* sendRequest.
*
* @desc Sends a web request to the api
*
* @param string $module
* @param string $action
* @param array $parameters A Key Value Store of Parameters
*
* @return SimpleXMLElement Response from Server
*
* @throws Exception
*/
private function sendRequest($module = 'prospect', $action = 'query', $parameters = array())
{
$baseUrl = 'https://pi.pardot.com/api/';
$version = 'version/3/';
if ($this->apiKey && $this->userKey) {
$login = array('api_key' => $this->apiKey,'user_key' => $this->userKey);
$parameters = array_merge($login, $parameters);
$action = 'do/'.$action;
}
if ($this->outputMode) {
$output = array('output' => $this->outputMode);
$parameters = array_merge($output, $parameters);
}
$url = $baseUrl.$module.'/'.$version.$action;
$context = stream_context_create(array(
'http' => array(
'method' => 'POST', //never want to send credentials over GET
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($parameters),
'timeout' => 30.0, //in seconds
'user_agent' => 'PardotPHPClient',
//'proxy' => '',
//'ignore_errors' => false,
),
));
$res = file_get_contents($url, false, $context);
$ret = simplexml_load_string($res);
if ($ret->err) {
throw new PardotConnectorException($ret->err.' '.$url.' '.http_build_query($parameters), '1');
}
return $ret;
}
}
class PardotConnectorException extends Exception
{
public function __construct($message = '', $code = 1)
{
parent::__construct($message, $code);
}
}