API Changes
Posted: Wed Jul 08, 2009 2:43 am
The API document can be found on the API page. This thread serves to highlight any changes which occur.
Chris,cpeel2300 wrote:Being a bit of a web noob, what's the 40-byte 'api key' and how is this generated?
Code: Select all
https://prowl.weks.net/publicapi/add?apikey=xxxx&application=test 1&event=test event&description=something happened&priority=1
Code: Select all
<?
$api_key = "xxxx";
$app_name = "App Name";
$event = "An Event";
$description = "I am a description\nnew line1\nline2";
$priority = 0;
$url = "https://prowl.weks.net/publicapi/add?apikey=".$api_key."&application=".$app_name."&event=".$event."&description=".$description."&priority=".$priority;
//replace spaces
$url = str_replace(" ","%20",$url);
//remove £
$url = str_replace("£","",$url);
//replace line breaks with 2 spaces
$url = str_replace("\n","%20%20",$url);
$handle = fopen($url, "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
Use urlencode() on $app_name, $event and $description.Thickey wrote:Anyone have any ideas?
Thanks, I should have checked that. Sorry...Thickey wrote:Chris,cpeel2300 wrote:Being a bit of a web noob, what's the 40-byte 'api key' and how is this generated?
If you log into your prowl account at https://prowl.weks.net/ under settings there is a new API section at the top of the page.
The API interface is much easer for me to get working than the previous way of interaction, I have already changed my PHP script, it only took 5 mins.
True, but if the response included a notification id the API could be extended to support viewing, updating and deleting notifications which could be used by AJAX clients to provide a feed of Growl or other notifications on a website, hence the suggestion for JSON output as well.jacobb wrote:A snippy roy fielding would argue that's more REST-like than RESTful, but that's getting overly nitty, and a real RESTful API is probably not very useful in this case. Switching to post to be closer to REST couldn't hurt, though
Code: Select all
<?php
class Prowl {
private $apikey;
private $application;
function Prowl($apikey, $application) {
$this->apikey = $apikey;
$this->application = $application;
}
function add($priority, $event, $description) {
$url = 'https://prowl.weks.net/publicapi/add?apikey=' . urlencode($this->apikey) .
'&priority=' . urlencode($priority) .
'&application=' . urlencode($this->application) .
'&event=' . urlencode($event) .
'&description=' . urlencode($description);
echo $url; // for debug purposes only
$ch = curl_init($url);
$output = curl_exec($ch);
curl_close($ch);
}
}
$test = new Prowl("%valid_key%", "Some App");
$test->add(0, "Foo", "Bar");
?>
Code: Select all
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Can I ask from a technical point of view what the advantages of using cURL are over using fopen (see my code above for an example). the fopen negotiates the https with out any problems at all. Am I missing something?silverone wrote:Code: Select all
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Code: Select all
Warning: fopen(https://prowl.weks.net/publicapi/add?apikey=...&priority=0&application=Some+App&event=Foo&description=Bar) [function.fopen]: failed to open stream: No such file or directory in /home/silver/public_html/prowl.php on line 21
OK, I thought I was doing something crazy. With PHP as in most programming languages there are many ways to do the same thing. I would suggest perhaps trying a different url to see if that works etc...silverone wrote:I was using the same approach as you in your prior post.
If the API changes to using POST (yes, please) to add messages, then fopen won't work since all it can do is GET. cURL does POST like a champ.Thickey wrote: Can I ask from a technical point of view what the advantages of using cURL are over using fopen (see my code above for an example). the fopen negotiates the https with out any problems at all. Am I missing something?
Code: Select all
$url = /* some valid prowl url */
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Code: Select all
<?php
class Prowl {
private $apikey;
private $application;
function Prowl($apikey, $application) {
$this->apikey = $apikey;
$this->application = $application;
$this->verify();
}
function add($priority, $event, $description) {
$options = array(
'apikey' => $this->apikey,
'priority' => $priority,
'application' => urlencode($this->application),
'event' => urlencode($event),
'description' => urlencode($description)
);
$this->request('https://prowl.weks.net/publicapi/add', $options);
}
function verify() {
$options = array('apikey' => $this->apikey);
$this->request('https://prowl.weks.net/publicapi/verify', $options);
}
private function request($file, $options) {
$url = $file;
$first = true;
foreach ($options as $key => $value) {
$url .= ($first ? '?' : '&') . $key . '=' . $value;
$first = false;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
curl_close($ch);
}
}
// Sample code
$test = new Prowl("0123456789abcdef0123456789abcdef01234567", "Some Application");
$test->add(0, "Event", "Description");
?>