-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasecampAPI.php
230 lines (205 loc) · 7.19 KB
/
BasecampAPI.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
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Project: New Basecamp PHP API
* File: Basecamp.php
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For questions, help, comments, discussion, etc., please join the
* Smarty mailing list. Send a blank e-mail to
*
* @link http://fedil.ukneeq.com/
* @copyright 2013 Ukneeq Solutions
* @author Fedil Grogan <fedil at ukneeq dot com>
* @package BasecampPHPAPI
* @version 0.5
*/
class basecamp
{
/** @type string Base url for bcx-api requests. */
protected $baseurl;
/** @type string Basecamp company/account ID. */
protected $id;
/** @type string Basecamp login username. */
protected $username;
/** @type string Basecamp login password. */
protected $password;
/** @type string User-Agent to identify Application to Basecamp. */
protected $useragent;
/** @type string String to store method errors. */
public $error;
/**
* This method creates a new Basecamp instance.
*
* @param int $id An unsigned integer.
* @param string $username A string that represents the login username;
* @param string $password A string that represets the login password;
*
*/
public function __construct($id, $username, $password, $appName, $email)
{
$this->id = $id;
$this->username = $username;
$this->password = $password;
$this->baseurl = "https://basecamp.com/$id/api/v1/";
$this->useragent = "User-Agent: $appName ($email)";
$this->error = "";
}
/* public methods */
/**
* Get all projects from Basecamp.
*
* @return array Array of project objects
*/
public function getProjects()
{
$request = $this->baseurl . "projects.json";
$result = $this->processRequest("GET", $request, array());
return $result;
}
/**
* Get all archived projects from Basecamp.
*
* @return array Array of project objects
*/
public function getArchivedProjects()
{
$request = $this->baseurl . "projects/archived.json";
$result = $this->processRequest("GET", $request, array());
return $result;
}
/**
* Get specific project from Basecamp.
*
* @return object Project object
*/
public function getProject($projectID)
{
$request = $this->baseurl . "projects/$projectID.json";
$result = $this->processRequest("GET", $request, array());
return $result;
}
/**
* Create project in Basecamp.
*
* @param string $name A String to store project name
* @param string $descript A String to store description
*
* @return object Project object
*/
public function createProject($name, $description)
{
$params = array("name" => "$name",
"description" => "$description");
$request = $this->baseurl . "projects.json";
$result = $this->processRequest("POST", $request, stripslashes(json_encode($params)));
return $result;
}
/**
* Update project in Basecamp.
*
* @param int $projectID Unsigned INT to store project id
* @param string $name A String to store project name
* @param string $descript A String to store description
*
* @return object Project object
*/
public function updateProject($projectID, $name, $description)
{
$params = array();
if ($name)
{
$params["name"] = $name;
}
if ($description)
{
$params["description"] = $description;
}
$request = $this->baseurl . "projects/$projectID.json";
$result = $this->processRequest("PUT", $request, stripslashes(json_encode($params)));
return $result;
}
/**
* Archive/Activate project in Basecamp.
*
* @param int $projectID Unsigned INT to represent basecamp project id.
* @param bool $archive A bool value to represent if project should be
* archived or activated.
*
* @return object Project object
*/
public function archiveProject($projectID, $archive)
{
$params = array("archived" => $archive);
$request = $this->baseurl . "projects/$projectID.json";
$result = $this->processRequest("PUT", $request, stripslashes(json_encode($params)));
return $result;
}
/* private methods */
private function processRequest($method, $request, $payload)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$request");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
curl_setopt($ch, CURLOPT_USERAGENT, "$this->useragent");
switch($method)
{
case "GET":
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
default:
$request_headers = array("Content-Type: application/json; charset=utf-8", "Accept: application/json");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (is_array($payload)) $payload = http_build_query($payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
break;
}
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) throw new Exception("cUrl error: $error", $errno);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
//print_r($message_body);
$response_headers = $this->curl_parse_headers($message_headers);
$statusCode = $response_headers['http_status_code'];
if ($statusCode >= 400)
{
throw new Exception("HTTP Error $statusCode:\n" . print_r(compact('method', 'request', 'request_headers', 'response'), 1));
}
return json_decode($message_body);
}
private function curl_parse_headers($message_headers)
{
$header_lines = preg_split("/\r\n|\n|\r/", $message_headers);
$headers = array();
list(, $headers['http_status_code'], $headers['http_status_message']) = explode(' ', trim(array_shift($header_lines)), 3);
foreach ($header_lines as $header_line)
{
list($name, $value) = explode(':', $header_line, 2);
$name = strtolower($name);
$headers[$name] = trim($value);
}
return $headers;
}
}
?>