Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update to v0.3

  • Loading branch information...
commit f8d97853c1fdf59aaa2ca9757abd17198219cab3 1 parent 35fcc3e
Fenric authored
Showing with 78 additions and 45 deletions.
  1. +65 −39 ProwlPHP.php
  2. +8 −3 README
  3. +5 −3 example.php
View
104 ProwlPHP.php
@@ -2,64 +2,73 @@
class Prowl
{
- private $_version = '0.2.2';
+ private $_version = '0.3';
private $_obj_curl = null;
- private $_verified = false;
private $_return_code;
private $_remaining;
private $_resetdate;
+
+ private $_use_proxy = false;
+ private $_proxy = null;
+ private $_proxy_userpwd = null;
private $_api_key = null;
- private $_api_domain = 'https://prowl.weks.net/publicapi';
- private $_url_verify = '/verify?apikey=%s';
- private $_url_push = '/add';
+ private $_prov_key = null;
+ private $_api_domain = 'https://prowl.weks.net/publicapi/';
+ private $_url_verify = 'verify?apikey=%s&providerkey=%s';
+ private $_url_push = 'add';
- private $_params = array( // Accessible params [key => maxsize]
- '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.
- 'description' => 10000, // Description of the event.
+ private $_params = array( // Accessible params [key => maxsize]
+ '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.
+ 'description' => 10000, // Description of the event.
);
- public function __construct($apikey, $providerkey=null)
+ public function __construct($apikey=null, $verify=false, $provkey=null, $proxy=null, $userpwd=null)
{
- $this->_api_key = $apikey;
- $return = $this->_execute(sprintf($this->_url_verify, $apikey));
+ if(isset($proxy))
+ $this->_setProxy($proxy, $userpwd);
- if($return===false)
- {
- $this->_error_code = 500;
- return false;
- }
+ if(isset($apikey) && $verify)
+ $this->verify($apikey, $provkey);
- $this->_verified = $this->_response($return);
+ $this->_api_key = $apikey;
}
- public function push($params, $is_post=false)
+ public function verify($apikey, $provkey)
{
- if(!$this->_verified)
- return false;
-
+ $return = $this->_execute(sprintf($this->_url_verify, $apikey, $provkey));
+ return $this->_response($return);
+ }
+
+ public function push($params, $is_post=false)
+ {
if($is_post)
$post_params = '';
$url = $is_post ? $this->_url_push : $this->_url_push . '?';
$params = func_get_args();
- $params[0]['apikey'] = $this->_api_key;
-
+
+ if(isset($this->_api_key) && !isset($params[0]['apikey']))
+ $params[0]['apikey'] = $this->_api_key;
+
+ if(isset($this->_prov_key) && !isset($params[0]['providerkey']))
+ $params[0]['providerkey'] = $this->_prov_key;
+
foreach($params[0] as $k => $v)
{
- $v = str_replace("\\n","\n",$v); // Fixes line break issue! Thank you Fr3d!
+ $v = str_replace("\\n","\n",$v); // Fixes line break issue! Cheers Fr3d!
if(!isset($this->_params[$k]))
{
- $this->_error_code = 400;
+ $this->_return_code = 400;
return false;
}
if(strlen($v) > $this->_params[$k])
{
- $this->_error_code = 10001;
+ $this->_return_code = 10001;
return false;
}
@@ -76,15 +85,9 @@ public function push($params, $is_post=false)
$return = $this->_execute($url, $is_post ? true : false, $params);
- if($return===false)
- {
- $this->_error_code=500;
- return false;
- }
-
return $this->_response($return);
}
-
+
public function getError()
{
switch($this->_return_code)
@@ -102,7 +105,7 @@ public function getError()
public function getRemaining()
{
- if(!$this->_verified)
+ if(!isset($this->_remaining))
return false;
return $this->_remaining;
@@ -110,7 +113,7 @@ public function getRemaining()
public function getResetDate()
{
- if(!$this->_verified)
+ if(!isset($this->_resetdate))
return false;
return $this->_resetdate;
@@ -131,6 +134,13 @@ private function _execute($url, $is_post=false, $params=null)
curl_setopt($this->_obj_curl, CURLOPT_POSTFIELDS, $params);
}
+ if($this->_use_proxy)
+ {
+ curl_setopt($this->_obj_curl, CURLOPT_HTTPPROXYTUNNEL, 1);
+ curl_setopt($this->_obj_curl, CURLOPT_PROXY, $this->_proxy);
+ curl_setopt($this->_obj_curl, CURLOPT_PROXYUSERPWD, $this->_proxy_userpwd);
+ }
+
$return = curl_exec($this->_obj_curl);
curl_close($this->_obj_curl);
return $return;
@@ -138,6 +148,12 @@ private function _execute($url, $is_post=false, $params=null)
private function _response($return)
{
+ if($return===false)
+ {
+ $this->_return_code = 500;
+ return false;
+ }
+
$response = new SimpleXMLElement($return);
if(isset($response->success))
@@ -159,6 +175,16 @@ private function _response($return)
unset($response);
}
+
+ private function _setProxy($proxy, $userpwd=null)
+ {
+ if(strlen($proxy) > 0)
+ {
+ $this->_use_proxy = true;
+ $this->_proxy = $proxy;
+ $this->_proxy_userpwd = $userpwd;
+ }
+ }
}
?>
View
11 README
@@ -10,6 +10,13 @@ I'd love to hear your ideas, suggestions and even your uses for ProwlPHP. Just d
------
Release Notes
------
+
+0.3
+* HTTP proxy support added.
+* Keys no longer verified be default at construction and as such saves API requests.
+* Provider keys now supported.
+* Error codes were not being saved correctly. Bug fixed.
+* API Key verification can now be called manually using 'verify' function.
0.2.2
@@ -27,6 +34,4 @@ Release Notes
Planned
------
-* Verify no longer required to use push function.
-* Provider keys
-* HTTP Proxy support
+* Optimize the code as much as possible!
View
8 example.php 100644 → 100755
@@ -6,9 +6,11 @@
$prowl->push(array(
'application'=>'Application',
'event'=>'Event',
- 'description'=>"Description",
- 'priority'=>0
- ));
+ 'description'=>'Test message! \n Sent at ' . date('H:i:s'),
+ 'priority'=>0,
+ //'apikey'=>'APIKEY' // Not required if already set during object construction.
+ //'providerkey'=>"PROVIDERKEY'
+ ),true);
var_dump($prowl->getError()); // Optional
var_dump($prowl->getRemaining()); // Optional
Please sign in to comment.
Something went wrong with that request. Please try again.