]> git.mxchange.org Git - friendica-addons.git/blob - posterous/posterous-api.php
Merge pull request #55 from CatoTH/master
[friendica-addons.git] / posterous / posterous-api.php
1 <?php /*
2 Name: Posterous API Library
3 Description: Object-oriented PHP class for accessing the Posterous API
4 Author: Calvin Freitas
5 Version: 0.1.0
6 Author URI: http://calvinf.com/
7 License:  MIT License (see LICENSE) http://creativecommons.org/licenses/MIT/
8 Warranties: None
9 Last Modified: December 07, 2009
10 Requirements: PHP 5 or higher.
11 */
12
13 /*
14 Copyright (c) 2009 Calvin Freitas
15
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 THE SOFTWARE.
33 */
34
35 /* Define Static Variables */
36 define('POSTEROUS_API_LIBRARY_VERSION','1.0');
37 define('POSTEROUS_API_LIBRARY_RELEASE_DATE','December 07, 2009');
38 define('POSTEROUS_API_LIBRARY_URL','http://calvinf.com/projects/posterous-api-library-php/');
39 define('POSTEROUS_API_LIBRARY_AUTHOR_NAME','Calvin Freitas');
40 define('POSTEROUS_API_LIBRARY_AUTHOR_URL','http://calvinf.com/');
41 define('POSTEROUS_API_LIBRARY_AUTHOR_EMAIL','cal@calvinfreitas.com');
42
43 define('POSTEROUS_API_URL', 'http://posterous.com/api/');
44
45 // ensure Curl extension installed
46 if(!extension_loaded("curl")) {
47         throw(new Exception("The Curl extension for PHP is required for PosterousAPI to work."));
48 }
49
50 /* Useful to catch this exception separately from standard PHP Exceptions */
51 class PosterousException extends Exception {}
52
53 /* This class contains functions for calling the Posterous API */
54 class PosterousAPI {
55         private $user;
56         private $pass;
57
58         function __construct($user = NULL, $pass = NULL) {
59                 $this->user = $user;
60                 $this->pass = $pass;
61         }
62
63         /* Reading Methods - http://posterous.com/api/reading */
64         function getsites() {
65                 $api_method = 'getsites';
66                 $xml = $this->_call( $api_method );
67                 return $xml;
68         }
69
70         function readposts($args) {
71                 $api_method = 'readposts';
72
73                 $valid_args = array('hostname','site_id','num_posts','page','tag');
74                 $method_args = $this->_validate($args, $valid_args);
75
76                 $xml = $this->_call( $api_method, $method_args );
77                 return $xml;
78         }
79
80         function gettags($args) {
81                 $api_method = 'gettags';
82
83                 $valid_args = array('hostname','site_id');
84                 $method_args = $this->_validate($args, $valid_args);
85
86                 $xml = $this->_call( $api_method, $method_args );
87                 return $xml;
88         }
89
90         /* Posting Methods - http://posterous.com/api/posting */
91         function newpost($args) {
92                 $api_method = 'newpost';
93
94                 if (!$this->_auth()) {
95                         throw new PosterousException('Posterous API call "' . $api_method . '" requires authentication.');
96                 }
97
98                 $valid_args = array('site_id','media','title','body','autopost','private','date','tags','source','sourceLink');
99                 $method_args = $this->_validate($args, $valid_args);
100
101                 $xml = $this->_call( $api_method, $method_args );
102                 return $xml;
103         }
104
105         function updatepost($args) {
106                 $api_method = 'updatepost';
107
108                 if (!$this->_auth()) {
109                         throw new PosterousException('Posterous API call "' . $api_method . '" requires authentication.');
110                 }
111
112                 $valid_args = array('post_id','media','title','body');
113                 $method_args = $this->_validate($args, $valid_args);
114
115                 $xml = $this->_call( $api_method, $method_args );
116                 return $xml;
117         }
118
119         function newcomment($args) {
120                 $api_method = 'newcomment';
121
122                 $valid_args = array('post_id','comment','name','email','date');
123                 $method_args = $this->_validate($args, $valid_args);
124
125                 $xml = $this->_call( $api_method, $method_args );
126                 return $xml;
127         }
128
129         /* Post.ly Methods - http://posterous.com/api/postly */
130
131         function getpost($args) {
132                 $api_method = 'getpost';
133
134                 $valid_args = array('id');
135                 $method_args = $this->_validate($args, $valid_args);
136
137                 $xml = $this->_call( $api_method, $method_args );
138                 return $xml;
139         }
140
141         /* Twitter Methods - http://posterous.com/api/twitter */
142         function upload() {
143                 $api_method = 'upload';
144
145                 $valid_args = array('username','password','media','message','body','source','sourceLink');
146                 $method_args = $this->_validate( $args, $method_args );
147
148                 $xml = $this->_call( $api_method, $method_args );
149                 return $xml;
150         }
151
152         function uploadAndPost() {
153                 $api_method = 'uploadAndPost';
154
155                 $valid_args = array('username','password','media','message','body','source','sourceLink');
156                 $method_args = $this->_validate( $args, $method_args );
157
158                 $xml = $this->_call( $api_method, $method_args );
159                 return $xml;
160         }
161
162
163         /* Helper Functions */
164         private function _call($api_method, $method_args = NULL) {
165                 $method_url = POSTEROUS_API_URL . $api_method;
166
167                 $user = $this->user();
168                 $pass = $this->pass();
169
170                 $ch = curl_init();
171                 curl_setopt($ch, CURLOPT_URL, $method_url);
172                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
173                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
174                 curl_setopt($ch, CURLOPT_HEADER, false);
175                 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
176
177                 if (isset($user) && isset($pass) && $user != '' && $pass != '') {
178                         curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
179                 }
180
181                 curl_setopt($ch, CURLOPT_POST, 1);
182
183                 if ( is_array($method_args) && !empty($method_args) ) {
184                         curl_setopt($ch, CURLOPT_POSTFIELDS, $method_args);
185                 }
186
187                 $data = curl_exec($ch);
188                 //$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
189                 curl_close($ch);
190
191                 $xml = '';
192                 try {
193                         $xml = new SimpleXMLElement($data);
194
195                         $response_status = $xml['stat'];
196                         if ($response_status == 'ok') {
197                                 return $xml;
198                         }
199                         elseif ($response_status == 'fail') {
200                                 throw new PosterousException('Error Code ' . $xml->err['code'] . ': ' . $xml->err['msg']);
201                         }
202                         else {
203                                 throw new PosterousException('Error: Invalid Posterous response status.');
204                         }
205                 }
206                 catch (Exception $e) {
207                         throw $e;
208                 }
209         }
210
211         private function _validate($args, $valid_args) {
212                 $method_args = array();
213                 foreach($args as $key => $value) {
214                         if( in_array($key, $valid_args) ) {
215                                 $method_args[$key] = $value;
216                         }
217                 }
218
219                 return $method_args;
220         }
221
222         private function _auth() {
223                 //checks if object has user & password, does not verify w/ Posterous
224                 if (isset($this->user) && isset($this->pass) && $this->user != '' && $this->pass != '') {
225                         return TRUE;
226                 }
227                 else {
228                         return FALSE;
229                 }
230         }
231
232         /* Getters & Setters */
233         function user($user = NULL) {
234                 if ($user) {
235                         $this->user = $user;
236                 }
237                 return $this->user;
238         }
239
240         function pass($pass = NULL) {
241                 if ($pass) {
242                         $this->pass = $pass;
243                 }
244                 return $this->pass;
245         }
246 }
247
248 ?>