slawcup / drop.io-api-php

A PHP Client library for the drop.io API

This URL has Read+Write access

drop.io-api-php / drop_io_api.php
100644 292 lines (254 sloc) 7.3 kb
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
291
292
<?php
/*
* PHP API Lib for drop.io
* Copyright (C) 2008, David Billingham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
*/
class DROPIO_Transfer{
const API_KEY='YOUR API KEY';
const USERAGENT='dropio-php-lib';
 
const RESPONSE_TYPE_XML='xml';
const RESPONSE_TYPE_JSON='json';
 
private $response_type='';
 
public function __construct(){
if(function_exists('json_decode')){
$this->response_type=self::RESPONSE_TYPE_JSON;
}elseif(function_exists('simplexml_load_string')){
$this->response_type=self::RESPONSE_TYPE_XML;
}else{
throw new Exception('proper decoding extension not installed. (json or simple xml)');
}
}
 
public function connect(DROPIO_ApiCall $call){
$call->api_key=DROPIO_Transfer::API_KEY;
$call->format=$this->response_type;
 
$call->isReady();
 
$ch = curl_init($call->getApiEndPoint());
curl_setopt($ch, CURLOPT_POSTFIELDS, $call->getPostArgs());
 
switch($call->getHTTPRequestType()){
case 'POST':
curl_setopt ($ch, CURLOPT_POST, true);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_POSTFIELDS, $call->getPostArgs());
break;
case 'PUT':
//curl_setopt ($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
break;
case 'GET':
curl_setopt ($ch, CURLOPT_HTTPGET, true);
curl_setopt ($ch, CURLOPT_URL, $call->getApiEndPoint().'?'.$call->getPostArgs());
}
 
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, DROPIO_Transfer::USERAGENT);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
//$response=explode("\r\n\r\n",curl_exec($ch),2);
$response = explode("\r\n\r\n", curl_exec($ch), 2);
 
curl_close($ch);
 
return $this->processResponse($response[1]);
}
 
private function processResponse($resp){
$value = array();
switch($this->response_type){
case self::RESPONSE_TYPE_JSON:
$value=$this->postProcessJson($resp);
case self::RESPONSE_TYPE_XML:
$value=$this->postProcessXml($resp);
}
return $value;
}
 
private function postProcessXml($xml){
//print_r(htmlspecialchars($xml));
$arr=get_object_vars(simplexml_load_string($xml));
return $arr;
}
 
private function postProcessJson($json){
return json_decode($json);
}
}
 
 
abstract class DROPIO_ApiCall{
protected $HTTP_REQUEST_TYPE='GET';
protected $API_ENDPOINT_URL='http://api.drop.io/';
protected $params=array('format'=>'json');
protected $required_params=array('api_key','format');
protected $uri_params=array();
abstract function buildURI();
protected function addRequiredParam($name){
if ( !in_array($name,$this->required_params)){
$this->required_params[]=$name;
}
}
protected function addURIParam($name){
if ( !in_array($name,$this->uri_params)){
$this->uri_params[]=$name;
}
}
protected function setHTTPRequestType($type){
$this->HTTP_REQUEST_TYPE=$type;
}
public function __set($property,$value){
$this->params[$property]=$value;
}
 
public function getHTTPRequestType(){
return $this->HTTP_REQUEST_TYPE;
}
 
public function getPostArgs(){
$qs=array();
foreach($this->params AS $key=>$value){
$qs[]=$key.'='.urlencode($value);
}
return implode('&',$qs);
}
 
public function getApiEndPoint(){
return $this->API_ENDPOINT_URL.$this->buildURI();
}
 
public function isReady(){
foreach ( $this->required_params AS $v ) {
if ( !in_array($v,$this->uri_params)
&& !array_key_exists($v,$this->params)){
throw new Exception(__CLASS__.' requires param "'.$v.'"');
}
}
}
 
protected function connectAndSend(){
$t = new DROPIO_Transfer();
return $t->connect($this);
}
abstract function send();
}
 
class DROPIO_CreateDrop extends DROPIO_ApiCall{
function __construct(){
parent::setHTTPRequestType('POST');
}
function buildURI(){
return 'drops';
}
function send(){
return new DROPIO_Drop(parent::connectAndSend());
}
}
 
class DROPIO_GetDrop extends DROPIO_ApiCall{
function __construct(){
parent::addURIParam('name');
parent::setHTTPRequestType('GET');
}
function buildURI(){
return 'drops/'.$this->params['name'];
}
function send(){
return new DROPIO_Drop(parent::connectAndSend());
}
}
 
class DROPIO_UpdateDrop extends DROPIO_ApiCall{
function __construct(){
parent::addURIParam('name');
parent::addRequiredParam('token');
parent::setHTTPRequestType('PUT');
}
function buildURI(){
return 'drops/'.$this->params['name'];
}
function send(){
return new DROPIO_Drop(parent::connectAndSend());
}
}
 
class DROPIO_DeleteDrop extends DROPIO_ApiCall{
function __construct(){
parent::addURIParam('name');
parent::addRequiredParam('token');
parent::setHTTPRequestType('DELETE');
}
function buildURI(){
return 'drops/'.$this->params['name'];
}
function send(){
return new DROPIO_Drop(parent::connectAndSend());
}
}
 
class DROPIO_CreateAsset_Link extends DROPIO_ApiCall{
function __construct(){
parent::setHTTPRequestType('POST');
parent::addURIParam('drop_name');
parent::addRequiredParam('url');
}
function buildURI(){
return 'drops/'.$this->params['drop_name'].'/assets';
}
function send(){
return new DROPIO_Asset(parent::connectAndSend());
}
}
 
class DROPIO_CreateAsset_Note extends DROPIO_ApiCall{
function __construct(){
parent::setHTTPRequestType('POST');
parent::addURIParam('drop_name');
parent::addRequiredParam('contents');
}
function buildURI(){
return 'drops/'.$this->params['drop_name'].'/assets';
}
function send(){
return new DROPIO_Asset(parent::connectAndSend());
}
}
 
class DROPIO_GetAsset extends DROPIO_ApiCall{
function __construct(){
parent::addURIParam('drop_name');
parent::addURIParam('name');
parent::setHTTPRequestType('GET');
}
function buildURI(){
return 'drops/'.$this->params['drop_name'].'/assets/'.$this->params['name'];
}
function send(){
return new DROPIO_Asset(parent::connectAndSend());
}
}
 
class DROPIO_GetAssets extends DROPIO_ApiCall{
function __construct(){
parent::addURIParam('drop_name');
parent::setHTTPRequestType('GET');
}
function buildURI(){
return 'drops/'.$this->params['drop_name'].'/assets';
}
function send(){
return parent::connectAndSend();
}
}
 
 
 
abstract class DROPIO_Resource{
private $values=array();
private $created_at=NULL;
function __construct($array){
foreach($array AS $k=>$v){
$k=str_replace('-','_',$k);
if(is_object($v)){
$this->values[$k]=get_object_vars($v);
}else{
$this->values[$k]=$v;
}
}
$this->created_at=microtime(true);
}
function __get($key){
return isset($this->values[$key])?$this->values[$key]:NULL;
}
}
 
class DROPIO_Drop extends DROPIO_Resource{}
 
class DROPIO_Asset extends DROPIO_Resource{}
 
class DROPIO_Comment extends DROPIO_Resource{}
 
 
 
?>