Isotopic

blog

20 Dec 2015

Social Media Feed Blender

Web Development

api, PHP, social media

Nowadays most companies have one or more social media accounts and I'm often asked to embed this source of content into their websites. Here's a PHP class that fetches all the desired feeds, mixes them up and generates a uniform, cacheable source.

For now four social networks are available: Facebook, Twitter, Instagram and Youtube. In the case of Facebook, Twitter and Youtube, an application must be registered in order to get the needed client_id and app_secret credentials. The ones in the example are for demonstration purposes only and will expire soon.

Facebook applications can be registered here, Twitter applications here and Youtube applications here.

Usage example:

require 'FeedBlender.php';
$feed_blender = new FeedBlender(
  array(
    'facebook'=>array(
      'client_id'=>'YOUR_FACEBOOK_CLIENT_ID',
      'app_secret'=>'YOUR_FACEBOOK_APP_SECRET',
      'users'=>array('mitmedialab')
    ),
    'instagram'=>array(
      'users'=>array('unsplash','iss')
    ),
    'twitter'=>array(
      'client_id'=>'YOUR_TWITTER_CLIENT_ID',
      'app_secret'=>'YOUR_TWITTER_APP_SECRET',
      'users'=>array('nasa')
    ),
    'youtube'=>array(
      'app_secret'=>'YOUR_YOUTUBE_APP_SECRET',
      'users'=>array('TEDEducation','Computerphile')
    )
  )
);
$response_json = $feed_blender->getFeed(30);

Results in something like:

{
  "status": "success",
  "message": "",
  "data": [
    {
      "source": "twitter",
      "username": "NASA",
      "id": 706291714785452032,
      "link": "https://twitter.com/NASA/status/706291714785452032",
      "timestamp": 1457228041,
      "created_time": "05 Mar 2016",
      "text": "After yrs of tests & development, scientific balloon is set to break flight duration record: https://t.co/9IdbMG8KtW https://t.co/x1sh3oA060",
      "image": "https://pbs.twimg.com/media/Cc1AkoeWIAApEjx.jpg"
    },
    {
      "source": "facebook",
      "username": "MIT Media Lab",
      "id": "51320424738_10154093003709739",
      "link": "http://facebook.com/51320424738/posts/10154093003709739",
      "timestamp": 1457224511,
      "created_time": "05 Mar 2016",
      "text": "Celebrating the Invisible Cryptologists: The Digital Currency Initiative's Gina Vargas on the African-Americans who were instrumental to developing early cryptography from WWII through 1956."
      ...   

Besides the benefit of simplification by abstracting all responses into a single collection, there's also a huge performance improvement from caching requests. When calling the APIs, all requests can take something like 10000 millisseconds -- while the access of cached content takes about 5 milliseconds. Furthermore, this prevents exceeding most API rate limits.

FeedBlender on GitHub