-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobilecommons.class.php
57 lines (43 loc) · 1.86 KB
/
mobilecommons.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
<?php
/**
* simple php SDK for Mobile Commons API
* verson 1.0 - January 2016
* author: Jonathan Kissam, jonathankissam.com
* API documentation: https://mobilecommons.zendesk.com/hc/en-us/articles/202052534-REST-API
*/
class MobileCommons {
private $username = 'PASS_USERNAME_WHEN_INSTANTIATING_CLASS';
private $passwd = 'PASS_USERNAME_WHEN_INSTANTIATING_CLASS';
private $subdomain = 'https://secure.mcommons.com';
public function __construct($username = null, $passwd = null, $subdomain = 'https://secure.mcommons.com') {
if(!extension_loaded('curl')) trigger_error('MobileCommons requires PHP cURL', E_USER_ERROR);
if(is_null($username)) trigger_error('username must be supplied', E_USER_ERROR);
$this->username = $username;
if(is_null($passwd)) trigger_error('passwd must be supplied', E_USER_ERROR);
$this->passwd = $passwd;
$this->subdomain = $subdomain;
}
public function call($endpoint, $method = 'GET', $parameters = array()) {
$url = $this->subdomain.'/api/'.$endpoint;
$parameters_query = str_replace('&','&',http_build_query($parameters));
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->passwd}");
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters_query);
} else {
if ($parameters_query) { $url .= '?'.$parameters_query; }
}
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function profile_update($parameters = array()) {
if (!isset($parameters['phone_number'])) trigger_error('phone_number must be supplied for MobileCommons->profile_update', E_USER_ERROR);
return $this->call('profile_update','POST',$parameters);
}
}