-
Notifications
You must be signed in to change notification settings - Fork 1
/
Highrise.php
145 lines (106 loc) · 2.97 KB
/
Highrise.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
<?php
/*
---
Highrise-PHP:
A PHP toolkit for accessing Highrise
version: 0.1.0
copyrights:
- [Ryan Mitchell](@ryanhmitchell)
license:
- [MIT License]
---
*/
class Highrise {
// credentials
private $token;
// base url for api calls
private $apiUrl = 'https://{sub}.highrisehq.com/';
// debug mode
private $debug = false;
// default constructor
function __construct($subdomain, $token){
// subdomain
$exploded = explode('.', $subdomain);
$subdomain = array_shift($exploded);
$this->apiUrl = str_replace('{sub}', $subdomain, $this->apiUrl);
// token
$this->token = $token;
}
public function get($endpoint, $data=array()){
return $this->call($endpoint, 'get', $data);
}
public function post($endpoint, $data){
return $this->call($endpoint, 'post', $data);
}
public function put($endpoint, $data){
return $this->call($endpoint, 'put', $data);
}
public function delete($endpoint, $data){
return $this->call($endpoint, 'delete', $data);
}
/**************************************************************************
* Private functions
**************************************************************************/
private function call($endpoint, $type, $data=array()){
$ch = curl_init();
// Setup curl options
$curl_options = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'Highrise-PHP',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array(
'Accept: application/xml',
'Content-Type: application/xml'
),
CURLOPT_USERPWD => $this->token.':x'
);
// Set curl url to call
$curlURL = $this->apiUrl.$endpoint.'.xml?';
// type of request determines our headers
switch($type){
case 'post':
$curl_options = $curl_options + array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data
);
break;
case 'put':
$curl_options = $curl_options + array(
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $data
);
break;
case 'delete':
$curl_options = $curl_options + array(
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => $data
);
break;
case 'get':
$curlURL .= '&'.http_build_query($data);
break;
}
// add url
$curl_options = $curl_options + array(
CURLOPT_URL => $curlURL
);
// Set curl options
curl_setopt_array($ch, $curl_options);
// Send the request
$this->result = curl_exec($ch);
// curl info
$this->info = curl_getinfo($ch);
if ($this->debug){
var_dump($this->result);
var_dump($this->info);
}
// Close the connection
curl_close($ch);
// load string to xml, convert to json and back again to give us std. objects
return json_decode(json_encode(simplexml_load_string($this->result)));
}
}
?>