Friendfeed API implementation in PHP


by Php Ninza on July 18, 2009

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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 5 comments… read them below or Shout @ me! }

phpcollection July 18, 2009 at 3:02 am

New blog post: Friendfeed API implementation in PHP http://bit.ly/TcRC7

This comment was originally posted on Twitter

OnMoneyMaker July 18, 2009 at 9:57 am

Friendfeed API implementation in PHP | PHP tutorials and Scripts …: Online money making scripts, Tutorials, PH.. http://bit.ly/KvtJt

This comment was originally posted on Twitter

webtechman July 26, 2009 at 4:12 pm

Google Friendfeed API implementation in #PHP http://bit.ly/8UXWW Cool Social Media/Web 2.0 tools #opensource

This comment was originally posted on Twitter

FOSSwiki July 26, 2009 at 4:38 pm

Google Friendfeed API implementation in #PHP http://bit.ly/8UXWW Cool Social Media/Web 2.0 tools #opensource http://bit.ly/tgZ5a

This comment was originally posted on Twitter

tek_news August 21, 2009 at 6:14 am

Reddit/p: Friendfeed API implementation in PHP http://bit.ly/yoZaD

This comment was originally posted on Twitter

Leave a Comment

{ 1 trackback }

Previous post:

Next post: