forked from devincrossman/mindbody-php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMB_API.php
212 lines (200 loc) · 7.64 KB
/
MB_API.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
<?php
class MB_API {
protected $client;
protected $sourceCredentials = array(
"SourceName"=>'REPLACE_WITH_YOUR_SOURCENAME',
"Password"=>'REPLACE_WITH_YOUR_PASSWORD',
"SiteIDs"=>array('REPLACE_WITH_YOUR_SITE_ID')
);
/*
** Uncomment if you need user credentials
protected $userCredentials = array(
"Username"=>'REPLACE_WITH_YOUR_USERNAME',
"Password"=>'REPLACE_WITH_YOUR_PASSWORD',
"SiteIDs"=>array('REPLACE_WITH_YOUR_SITE_ID')
);
*/
protected $appointmentServiceWSDL = "https://api.mindbodyonline.com/0_5/AppointmentService.asmx?WSDL";
protected $classServiceWSDL = "https://api.mindbodyonline.com/0_5/ClassService.asmx?WSDL";
protected $clientServiceWSDL = "https://api.mindbodyonline.com/0_5/ClientService.asmx?WSDL";
protected $dataServiceWSDL = "https://api.mindbodyonline.com/0_5/DataService.asmx?WSDL";
protected $finderServiceWSDL = "https://api.mindbodyonline.com/0_5/FinderService.asmx?WSDL";
protected $saleServiceWSDL = "https://api.mindbodyonline.com/0_5/SaleService.asmx?WSDL";
protected $siteServiceWSDL = "https://api.mindbodyonline.com/0_5/SiteService.asmx?WSDL";
protected $staffServiceWSDL = "https://api.mindbodyonline.com/0_5/StaffService.asmx?WSDL";
protected $apiMethods = array();
protected $apiServices = array();
public $soapOptions = array('soap_version'=>SOAP_1_1, 'trace'=>true);
public $debugSoapErrors = true;
/*
** initializes the apiServices and apiMethods arrays
*/
public function __construct($sourceCredentials = array()) {
// set apiServices array with Mindbody WSDL locations
$this->apiServices = array(
'AppointmentService' => $this->appointmentServiceWSDL,
'ClassService' => $this->classServiceWSDL,
'ClientService' => $this->clientServiceWSDL,
'DataService' => $this->dataServiceWSDL,
'FinderService' => $this->finderServiceWSDL,
'SaleService' => $this->saleServiceWSDL,
'SiteService' => $this->siteServiceWSDL,
'StaffService' => $this->staffServiceWSDL
);
// set apiMethods array with available methods from Mindbody services
foreach($this->apiServices as $serviceName => $serviceWSDL) {
$this->client = new SoapClient($serviceWSDL, $this->soapOptions);
$this->apiMethods = array_merge($this->apiMethods, array($serviceName=>array_map(
function($n){
$start = 1+strpos($n, ' ');
$end = strpos($n, '(');
$length = $end - $start;
return substr($n, $start, $length);
}, $this->client->__getFunctions()
)));
}
// set sourceCredentials
if(!empty($sourceCredentials)) {
if(!empty($sourceCredentials['SourceName'])) {
$this->sourceCredentials['SourceName'] = $sourceCredentials['SourceName'];
}
if(!empty($sourceCredentials['Password'])) {
$this->sourceCredentials['Password'] = $sourceCredentials['Password'];
}
if(!empty($sourceCredentials['SiteIDs'])) {
if(is_array($sourceCredentials['SiteIDs'])) {
$this->sourceCredentials['SiteIDs'] = $sourceCredentials['SiteIDs'];
} else if(is_numeric($sourceCredentials['SiteIDs'])) {
$this->sourceCredentials['SiteIDs'] = array($sourceCredentials['SiteIDs']);
}
}
}
}
/*
** magic method will search $this->apiMethods array for $name and call the
** appropriate Mindbody API method if found
*/
public function __call($name, $arguments) {
// check if method exists on one of mindbody's soap services
$soapService = false;
foreach($this->apiMethods as $apiServiceName=>$apiMethods) {
if(in_array($name, $apiMethods)) {
$soapService = $apiServiceName;
}
}
if(!empty($soapService)) {
if(empty($arguments)) {
return $this->callMindbodyService($soapService, $name);
} else {
switch(count($arguments)) {
case 1:
return $this->callMindbodyService($soapService, $name, $arguments[0]);
case 2:
return $this->callMindbodyService($soapService, $name, $arguments[0], $arguments[1]);
case 3:
return $this->callMindbodyService($soapService, $name, $arguments[0], $arguments[1], $arguments[2]);
}
}
} else {
echo "called unknown method '$name'<br />";
}
}
/*
** return the results of a Mindbody API method
**
** string $serviceName - Mindbody Soap service name
** string $methodName - Mindbody API method name
** array $requestData - Optional: parameters to API methods
** boolean $returnObject - Optional: Return the SOAP response object
*/
protected function callMindbodyService($serviceName, $methodName, $requestData = array(), $returnObject = false, $debugErrors = false) {
$request = array_merge(array("SourceCredentials"=>$this->sourceCredentials),$requestData);
if(!empty($this->userCredentials)) {
$request = array_merge(array("UserCredentials"=>$this->userCredentials), $request);
}
$this->client = new SoapClient($this->apiServices[$serviceName], $this->soapOptions);
try {
$result = $this->client->$methodName(array("Request"=>$request));
if($returnObject) {
return $result;
} else {
return json_decode(json_encode($result),1);
}
} catch (SoapFault $s) {
if($this->debugSoapErrors && $debugErrors) {
echo 'ERROR: [' . $s->faultcode . '] ' . $s->faultstring;
$this->debug();
return false;
}
} catch (Exception $e) {
if($this->debugSoapErrors && $debugErrors) {
echo 'ERROR: ' . $e->getMessage();
return false;
}
}
}
public function getXMLRequest() {
return $this->client->__getLastRequest();
}
public function getXMLResponse() {
return $this->client->__getLastResponse();
}
public function debug() {
echo "<textarea>".print_r($this->getXMLRequest(),1)."</textarea>";
echo "<textarea>".print_r($this->getXMLResponse(),1)."</textarea>";
}
public function makeNumericArray($data) {
return (isset($data[0])) ? $data : array($data);
}
public function replace_empty_arrays_with_nulls(array $array) {
foreach($array as &$value) {
if(is_array($value)) {
if(empty($value)) {
$value = null;
} else {
$value = $this->replace_empty_arrays_with_nulls($value);
}
}
}
return $array;
}
public function FunctionDataXml() {
$passed = func_get_args();
$request = empty($passed[0]) ? null : $passed[0];
$returnObject = empty($passed[1]) ? null : $passed[1];
$debugErrors = empty($passed[2]) ? null : $passed[2];
$data = $this->callMindbodyService('DataService', 'FunctionDataXml', $request);
$xmlString = $this->getXMLResponse();
$sxe = new SimpleXMLElement($xmlString);
$sxe->registerXPathNamespace("mindbody", "http://clients.mindbodyonline.com/api/0_5");
$res = $sxe->xpath("//mindbody:FunctionDataXmlResponse");
if($returnObject) {
return $res[0];
} else {
return $this->replace_empty_arrays_with_nulls(json_decode(json_encode($res[0]),1));
}
}
/*
** overrides SelectDataXml method to remove some invalid XML element names
**
** string $query - a TSQL query
*/
public function SelectDataXml($query, $returnObject = false, $debugErrors = false) {
$result = $this->callMindbodyService('DataService', 'SelectDataXml', array('SelectSql'=>$query), $returnObject, $debugErrors);
$xmlString = $this->getXMLResponse();
// replace some invalid xml element names
$xmlString = str_replace("Current Series", "CurrentSeries", $xmlString);
$xmlString = str_replace("Item#", "ItemNum", $xmlString);
$xmlString = str_replace("Massage Therapist", "MassageTherapist", $xmlString);
$xmlString = str_replace("Workshop Instructor", "WorkshopInstructor", $xmlString);
$sxe = new SimpleXMLElement($xmlString);
$sxe->registerXPathNamespace("mindbody", "http://clients.mindbodyonline.com/api/0_5");
$res = $sxe->xpath("//mindbody:SelectDataXmlResponse");
if($returnObject) {
return $res[0];
} else {
return $this->replace_empty_arrays_with_nulls(json_decode(json_encode($res[0]),1));
}
}
}
?>