2 Name: Posterous API Library
3 Description: Object-oriented PHP class for accessing the Posterous API
6 Author URI: http://calvinf.com/
7 License: MIT License (see LICENSE) http://creativecommons.org/licenses/MIT/
9 Last Modified: December 07, 2009
10 Requirements: PHP 5 or higher.
14 Copyright (c) 2009 Calvin Freitas
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:
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
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
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');
43 define('POSTEROUS_API_URL', 'http://posterous.com/api/');
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."));
50 /* Useful to catch this exception separately from standard PHP Exceptions */
51 class PosterousException extends Exception {}
53 /* This class contains functions for calling the Posterous API */
58 function __construct($user = NULL, $pass = NULL) {
63 /* Reading Methods - http://posterous.com/api/reading */
65 $api_method = 'getsites';
66 $xml = $this->_call( $api_method );
70 function readposts($args) {
71 $api_method = 'readposts';
73 $valid_args = array('hostname','site_id','num_posts','page','tag');
74 $method_args = $this->_validate($args, $valid_args);
76 $xml = $this->_call( $api_method, $method_args );
80 function gettags($args) {
81 $api_method = 'gettags';
83 $valid_args = array('hostname','site_id');
84 $method_args = $this->_validate($args, $valid_args);
86 $xml = $this->_call( $api_method, $method_args );
90 /* Posting Methods - http://posterous.com/api/posting */
91 function newpost($args) {
92 $api_method = 'newpost';
94 if (!$this->_auth()) {
95 throw new PosterousException('Posterous API call "' . $api_method . '" requires authentication.');
98 $valid_args = array('site_id','media','title','body','autopost','private','date','tags','source','sourceLink');
99 $method_args = $this->_validate($args, $valid_args);
101 $xml = $this->_call( $api_method, $method_args );
105 function updatepost($args) {
106 $api_method = 'updatepost';
108 if (!$this->_auth()) {
109 throw new PosterousException('Posterous API call "' . $api_method . '" requires authentication.');
112 $valid_args = array('post_id','media','title','body');
113 $method_args = $this->_validate($args, $valid_args);
115 $xml = $this->_call( $api_method, $method_args );
119 function newcomment($args) {
120 $api_method = 'newcomment';
122 $valid_args = array('post_id','comment','name','email','date');
123 $method_args = $this->_validate($args, $valid_args);
125 $xml = $this->_call( $api_method, $method_args );
129 /* Post.ly Methods - http://posterous.com/api/postly */
131 function getpost($args) {
132 $api_method = 'getpost';
134 $valid_args = array('id');
135 $method_args = $this->_validate($args, $valid_args);
137 $xml = $this->_call( $api_method, $method_args );
141 /* Twitter Methods - http://posterous.com/api/twitter */
143 $api_method = 'upload';
145 $valid_args = array('username','password','media','message','body','source','sourceLink');
146 $method_args = $this->_validate( $args, $method_args );
148 $xml = $this->_call( $api_method, $method_args );
152 function uploadAndPost() {
153 $api_method = 'uploadAndPost';
155 $valid_args = array('username','password','media','message','body','source','sourceLink');
156 $method_args = $this->_validate( $args, $method_args );
158 $xml = $this->_call( $api_method, $method_args );
163 /* Helper Functions */
164 private function _call($api_method, $method_args = NULL) {
165 $method_url = POSTEROUS_API_URL . $api_method;
167 $user = $this->user();
168 $pass = $this->pass();
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);
177 if (isset($user) && isset($pass) && $user != '' && $pass != '') {
178 curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
181 curl_setopt($ch, CURLOPT_POST, 1);
183 if ( is_array($method_args) && !empty($method_args) ) {
184 curl_setopt($ch, CURLOPT_POSTFIELDS, $method_args);
187 $data = curl_exec($ch);
188 //$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
193 $xml = new SimpleXMLElement($data);
195 $response_status = $xml['stat'];
196 if ($response_status == 'ok') {
199 elseif ($response_status == 'fail') {
200 throw new PosterousException('Error Code ' . $xml->err['code'] . ': ' . $xml->err['msg']);
203 throw new PosterousException('Error: Invalid Posterous response status.');
206 catch (Exception $e) {
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;
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 != '') {
232 /* Getters & Setters */
233 function user($user = NULL) {
240 function pass($pass = NULL) {