Saturday, July 18, 2009

Delicious Lite extension for Firefox 3.5

Today I decided to discontinue developing the Delicious Lite Firefox extension because the code is getting outdated and I don't have time to update it for the newly released Firefox 3.5. The good news is there's another new delicious bookmark extension that pretty much does the same thing (and better :). Check out the Delicious Post extension from AE Creations. This extension also stems from the original work of Patrick H. Lauke which I also used in Delicious Lite. So please try it out and kudos to AE Creations for such great work!

There are some stuff that I feel Delicious Post still lacks like a shortcut key and some super minor UI issues. To solve these issues for me I created a modified version which adds a shortcut key CTRL+SHIFT+D to post a bookmark (btw I borrowed this shortcut key from Firefox's Bookmark all tabs which I don't use much, and transfered it to CTRL+SHIFT+K). On the post dialog, I also made it focus on the tags textbox immediately because that's just usually what I edit when posting a bookmark. If you like these modifications, feel free to download the version here. I hope these will also get implemented on the official release :)

Saturday, July 4, 2009

Simple PHP Rest Client using cURL

I recently worked with a third party API that uses REST. I've been reading about REST all this time and this is the first time I've actually used it. Surprised me because it was really nice and simple. It's a lot easier trying it out than reading about it (but it sure helps to read some about it first :). What took quite some time was finding a REST client that I will use to connect and some authorization issues (which is site specific).

If you're already using Zend Framework, you might want to try out it's built in rest client. But if you're looking for a simpler solution, you can do well with PHP's cURL functions. Honestly I don't like cURL because it really looks dirty when you use it (aesthetically that is), and all the options you need to set kinda bums me. It's like you have to research a lot of option flags just to make something work. But hey, it almost always gets the job done :) Good thing iContact's API documentation (the API I'm working with) has a downloadable sample code that uses plain cURL functions to connect to it's REST service. I have to credit them for most of the code. I just did some minor edits so it is more generalized. So here, try it out if it works for you (this is plain PHP code, hopefully you'll put this in a function or method :)

This example is specific to JSON input/output, so you have to change some stuff if the API requires XML or some other format.

$url = 'https://www.google.com';
$method = 'POST';

# headers and data (this is API dependent, some uses XML)
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$data = json_encode(array(
'firstName'=> 'John',
'lastName'=> 'Doe'
));

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

switch($method)
{

case 'GET':
break;

case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;

case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;

case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}

$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);