-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimeo.class.php
290 lines (239 loc) · 6.92 KB
/
vimeo.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* File: api-vimeo
* Handle the Vimeo Simple API.
*
* Version:
* 2009.12.14
*
* Copyright:
* 2009 Jay Williams
*
* License:
* Simplified BSD License - http://opensource.org/licenses/bsd-license.php
*/
/*%******************************************************************************************%*/
// EXCEPTIONS
class Vimeo_Exception extends Exception {}
/*%******************************************************************************************%*/
// CONSTANTS
/**
* Constant: VIMEO_NAME
* Name of the software.
*/
define('VIMEO_NAME', 'api-vimeo');
/**
* Constant: VIMEO_VERSION
* Version of the software.
*/
define('VIMEO_VERSION', '1.0');
/**
* Constant: VIMEO_BUILD
* Build ID of the software.
*/
define('VIMEO_BUILD', gmdate('YmdHis', strtotime(substr('$Date$', 7, 25)) ? strtotime(substr('$Date$', 7, 25)) : filemtime(__FILE__)));
/**
* Constant: VIMEO_URL
* URL to learn more about the software.
*/
define('VIMEO_URL', 'http://github.com/jaywilliams/vimeo/');
/**
* Constant: VIMEO_USERAGENT
* User agent string used to identify the software
*/
define('VIMEO_USERAGENT', VIMEO_NAME . '/' . VIMEO_VERSION . ' (Vimeo Toolkit; ' . VIMEO_URL . ') Build/' . VIMEO_BUILD);
/*%******************************************************************************************%*/
// CLASS
/**
* Class: Vimeo
*/
class Vimeo
{
/**
* Property: subclass
* The API subclass (e.g. album, artist, user) to point the request to.
*/
var $subclass;
/**
* Property: output
* The output format (e.g. XML, JSON, PHP)
*/
var $output;
/**
* Property: test_mode
* Whether we're in test mode or not.
*/
var $test_mode;
/**
* Property: api_version
* The supported API version. This is inherited by all service-specific classes.
*/
var $api_version = null;
/**
* Property: set_hostname
* Stores the alternate hostname to use, if any. This is inherited by all service-specific classes.
*/
// var $hostname = null;
/*%******************************************************************************************%*/
// CONSTRUCTOR
/**
* Method: __construct()
* The constructor.
*
* Access:
* public
*
* Parameters:
* key - _string_ (Optional) Your Vimeo API Key. If blank, it will look for the <AWS_KEY> constant.
* secret_key - _string_ (Optional) Your Vimeo API Secret Key. If blank, it will look for the <AWS_SECRET_KEY> constant.
* subclass - _string_ (Optional) Don't use this. This is an internal parameter.
*
* Returns:
* boolean FALSE if no valid values are set, otherwise true.
*/
public function __construct($subclass = null)
{
// Set default values
$this->subclass = (array) $subclass;
$this->output = 'xml';
$this->api_version = 'v2';
return true;
}
/*%******************************************************************************************%*/
// SETTERS
/**
* Method: set_api_version()
* Sets a new API version to use in the request.
*
* Parameters:
* api_version - _string_ (Required) The version to use (e.g. 2.0).
*
* Returns:
* void
*/
public function set_api_version($api_version)
{
$this->api_version = $api_version;
}
/**
* Method: test_mode()
* Enables test mode within the API. Enabling test mode will return the request URL instead of requesting it.
*
* Access:
* public
*
* Parameters:
* enabled - _boolean_ (Optional) Whether test mode is enabled or not.
*
* Returns:
* void
*/
public function test_mode($enabled = true)
{
// Set default values
$this->test_mode = $enabled;
}
/*%******************************************************************************************%*/
// MAGIC METHODS
/**
* Handle requests to properties
*/
public function __get($var)
{
// Determine the name of this class
$class_name = get_class($this);
$subclass = (array) $this->subclass;
$subclass[] = strtolower($var);
// Re-instantiate this class, passing in the subclass value
$ref = new $class_name($subclass);
$ref->test_mode($this->test_mode); // Make sure this gets passed through.
$ref->set_api_version($this->api_version); // Make sure this gets passed through.
return $ref;
}
/**
* Handle requests to methods
*/
public function __call($name, $args)
{
// Change the names of the methods to match what the API expects
$name = strtolower($name);
$path = (count($this->subclass) > 0)? implode('/',$this->subclass) . '/' : '';
$method = $name . '.' . $this->output;
// Construct the rest of the query parameters with what was passed to the method
$query = ((count($args) > 0))? '?' . http_build_query($args[0], '', '&') : '';
$version = (!empty($this->api_version))? $this->api_version . '/' : '';
// Construct the URL to request
$api_call = 'http://vimeo.com/api/' . $version . $path . $method . $query;
// Return the value
return $this->request($api_call);
}
/**
* Method: oembed()
* Requests the oEmbed code for the specified video URL.
* Due to the Vimeo API, you must remove the API version number before you submit a oEmbed request.
* This method temporarily removes the API version number, and resets it after the request has finished.
* Yes, this is an unfortunate hack.
*
* Parameters:
* args - _array_ (Required) Must contain the array key 'url' for oembed lookup
*
* Returns:
* ResponseCore object
*/
public function oembed($args = null)
{
// Save Current API Version
$version = $this->api_version;
// Temporarily remove API Version for the oembed() request
$this->set_api_version(null);
$result = $this->__call('oembed', array($args));
// Reset the API Version
$this->set_api_version($version);
return $result;
}
/*%******************************************************************************************%*/
// REQUEST/RESPONSE
/**
* Method: request()
* Requests the data, parses it, and returns it. Requires RequestCore and SimpleXML.
*
* Parameters:
* url - _string_ (Required) The web service URL to request.
*
* Returns:
* ResponseCore object
*/
public function request($url)
{
if (!$this->test_mode)
{
if (class_exists('RequestCore'))
{
$http = new RequestCore($url);
$http->set_useragent(VIMEO_USERAGENT);
$http->send_request();
$response = new stdClass();
$response->header = $http->get_response_header();
$response->body = $this->parse_response($http->get_response_body());
$response->status = $http->get_response_code();
return $response;
}
throw new Exception('This class requires RequestCore. http://github.com/skyzyx/requestcore.');
}
return $url;
}
/**
* Method: parse_response()
* Default method for parsing the response data. You can extend the class and override this method for other response types.
*
* Parameters:
* data - _string_ (Required) The data to parse.
*
* Returns:
* mixed data
*/
public function parse_response($data)
{
return new SimpleXMLElement($data, LIBXML_NOCDATA);
}
}