Friendfeed API implementation in PHP

Recently for a client i needed to make a friendfinder script via Friendfeed API !, On giving a initial look at their API homepage , i was happy to see that Friendfeed provides client libraries for PHP development and their API is REST based.

However, on going through the PHP Client library i realized that they are not providing support for authenticated function calls in the library, That means anyone using their client library will not be able to use functionality of subscribing , unsubscribing and other user account specific authenticated calls. (Hope Friendfeed API developer guys are listening)

In the following tutorial i will list out all the functionalities friendfeed API support and will explain methods to call them from a PHP code.

Before we start talking about various API functionality , We need the understanding of CURL php library. To make things easier i used a PHP class Simple Curl Wrapper.

However i need to hack the MyCurl Class to enable API authentication, The modified Class file can be found here .

The modifications i did in Curl Class are described below(In case you want to use a fresh class file):-
The constructor defined for MyCurl.php class was as


function MyCurl()
{
    $this->fCookieFile = tempnam("/tmp", "g_");
}

I changed the constructor to accept the friendfeed username and api key so that authenticated REST calls can be made easily , The changed code is


function MyCurl($auth_nickname , $auth_key)
{
    $this->fCookieFile = tempnam("/tmp", "g_");
    $this->auth_nickname = $auth_nickname;
    $this->auth_key = $auth_key;
}

the code includes $auth_nickname which is the friendfeed username and the $auth_key which is the friendfeed user remote key, Remote key can be obtained via https://friendfeed.com/account/api

Also in the CURL php class , there is a post function defined which is used to pass $_POST variables on php pages, This function needs to patch up with the ability to pass username and remote key for http authentication , For this i used some extra curlopt values in the function


if ($this->auth_nickname && $this->auth_key) {

$this->setopt ( CURLOPT_USERPWD, $this->auth_nickname . ":" . $this->auth_key);
$this->setopt (CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  }

Friendfeed API need a Application api key (Application API key can be obtained via mailing at api@friendfeed.com !While mailing the request remember to specify Name of application, Username and the URL on which friendfeed api will run ) to create some account specific requests , Such as Subscribe a user , Unsubscribe a user etc…

Now let’s see some basic friendfeed API calls, I will be explaining all the methods in a series of blog posts. In the current blog post subscription and unsubscription to a user profile is explained

1) Subscribe to a user

For subscribing to the user , You need to pass the friendfeed nickname of the user in the api call, Also since this is a Account specific API call, it need to Post the api key value to the API call to get correct response, this can be done easily via .


$url = "http://friendfeed.com/api/user/$nickname/subscribe?format=xml"; //API call request
$post_data = array();
$post_data['apikey'] = $api_key;
$response = $mc->post($url, $post_data, null, $http_code);

In the above mentioned API request , variable $nickname is the user whom you want to subscribe and the $api_key is the key we have obtained via mailing at api@friendfeed.com .

Once this request is made the $response will contain the status of the request made , The result status will be either subscribed, unsubscribed, or requested . Also , the variable $http_code will contain the HTTP response code !, $http_code is pretty useful to understand the status of API calls !

2) Unsubscribing a user

Similarly for making a unsubscription we can use :-



$url = "http://friendfeed.com/api/user/$nickname/subscribe?unsubscribe=1&format=xml";
$post_data = array();
$post_data['apikey'] = $api_key;
$response = $mc->post($url_2, $post_data, null, $http_code);

Here also, the $response array contains the status of subscription which is subscribed, unsubscribed, or requested and $http_code array will contain the HTTP response of the API call.

Still there are many API calls that are not mentioned in the blog post, I will be covering them one by one in the future blog posts.

Please let me know your thoughts and response via comment on the post

6 thoughts on “Friendfeed API implementation in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>