Skip to content

Commit

Permalink
Update to v0.2.1. See revision notes in wiki.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenric committed Jul 11, 2009
1 parent f233040 commit d4aaadb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 41 deletions.
99 changes: 59 additions & 40 deletions ProwlPHP.php
Expand Up @@ -2,52 +2,50 @@

class Prowl
{
private $_version = '0.2';
private $_version = '0.2.1';
private $_obj_curl = null;
private $_verified = false;
private $_error_code = null;
private $_return_code;
private $_remaining;
private $_resetdate;

private $_api_key = null;
private $_api_domain = 'https://prowl.weks.net/publicapi';
private $_url_verify = '/verify?apikey=%s';
private $_url_push = '/add';

private $_params = array( // Accessible params [key => maxsize]
'apikey' => 40, // User API Key.
'priority' => 2, // Range from -2 to 2.
'apikey' => 40, // User API Key.
//'providerkey' => 40, // Provider key.
'priority' => 2, // Range from -2 to 2.
'application' => 254, // Name of the app.
'event' => 1024, // Name of the event.
'event' => 1024, // Name of the event.
'description' => 10000, // Description of the event.
);

public function __construct($apikey)
public function __construct($apikey, $providerkey=null)
{
$this->_api_key = $apikey;

$url = sprintf($this->_url_verify, $apikey);
$return = $this->_execute($url);
$return = $this->_execute(sprintf($this->_url_verify, $apikey));

if($return===false)
{
$this->_error_code=500;
$this->_error_code = 500;
return false;
}

$resp = new SimpleXMLElement($return);
$this->_verified = $this->_response($resp);
$this->_verified = $this->_response($return);
}

public function push($params, $is_post=false)
{
if(!$this->_verified)
return 'Auth Failed';
return false;

if(!$is_post)
{
$url = $is_post ? $this->_url_push : $this->_url_push . '?';
if($is_post)
$post_params = '';
}

$url = $is_post ? $this->_url_push : $this->_url_push . '?';
$params = func_get_args();
$params[0]['apikey'] = $this->_api_key;

Expand All @@ -63,44 +61,60 @@ public function push($params, $is_post=false)
$this->_error_code = 10001;
return false;
}
if(!$is_post)
{
$url .= $k . '=' . urlencode($v) . '&';
}
else
{

if($is_post)
$post_params .= $k . '=' . urlencode($v) . '&';
}
else
$url .= $k . '=' . urlencode($v) . '&';
}

if(!$is_post)
{
if($is_post)
$params = substr($post_params, 0, strlen($post_params)-1);
else
$url = substr($url, 0, strlen($url)-1);
}
else

$return = $this->_execute($url, $is_post ? true : false, $params);

if($return===false)
{
$params = substr($post_params, 0, strlen($post_params)-1);
$this->_error_code=500;
return false;
}

$return = $this->_execute($url, $is_post ? true : false, $params);
$resp = new SimpleXMLElement($return);
return $this->_response($resp);
return $this->_response($return);
}

public function getError()
{
switch($this->_error_code)
switch($this->_return_code)
{
case 200: return 'Pushed Successfully'; break;
case 200: return 'Request Successfull.'; break;
case 400: return 'Bad request, the parameters you provided did not validate.'; break;
case 401: return 'The API key given is not valid, and does not correspond to a user.'; break;
case 405: return 'Method not allowed, you attempted to use a non-SSL connection to Prowl.'; break;
case 406: return 'Your IP address has exceeded the API limit.'; break;
case 500: return 'Internal server error, something failed to execute properly on the Prowl side.'; break;
case 10001: return 'Parameter value exceeds the maximum byte size.'; break;
default: return false; break;
}
}

public function getRemaining()
{
if(!$this->_verified)
return false;

return $this->_remaining;
}

public function getResetDate()
{
if(!$this->_verified)
return false;

return $this->_resetdate;
}

private function _execute($url, $is_post=false, $params=null)
{
$this->_obj_curl = curl_init($this->_api_domain . $url);
Expand All @@ -121,23 +135,28 @@ private function _execute($url, $is_post=false, $params=null)
return $return;
}

private function _response($response)
private function _response($return)
{
$response = new SimpleXMLElement($return);

if(isset($response->success))
{
$code = $response->success['code'];
$this->_return_code = (int)$response->success['code'];
$this->_remaining = (int)$response->success['remaining'];
$this->_resetdate = (int)$response->success['resetdate'];
}
else
{
$code = $response->error['code'];
$this->_return_code = $response->error['code'];
}
$this->_error_code = $code;

switch($code)
switch($this->_return_code)
{
case 200: return true; break;
default: return false; break;
}

unset($response);
}
}

Expand Down
8 changes: 7 additions & 1 deletion example.php
@@ -1,11 +1,17 @@
<?php

include('ProwlPHP.php');

$prowl = new Prowl('APIKEY');
$prowl->push(array(
'application'=>'Application',
'event'=>'Event',
'description'=>"Description",
'description'=>"Description\nmoney\nmeow",
'priority'=>0
));

var_dump($prowl->getError()); // Optional
var_dump($prowl->getRemaining()); // Optional
var_dump(date('d m Y h:i:s', $prowl->getResetdate())); // Optional

?>

0 comments on commit d4aaadb

Please sign in to comment.