
Nowadays, most companies have one or more social media accounts, and I'm often asked to embed this content into their websites. Here's a PHP class that fetches all the desired feeds, mixes them, and generates a uniform, cacheable source.
Currently, four social networks are supported: Facebook, Twitter, Instagram, and YouTube. For Facebook, Twitter, and YouTube, you must register an application to obtain the required
client_id
and app_secret
credentials. The ones included in the example are for demonstration purposes only and will expire soon.
You can register Facebook applications 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 significant performance improvement thanks to request caching. Cached API calls take about 5 milliseconds, compared to 10,000 milliseconds for non-cached ones. Furthermore, this helps prevent exceeding most API rate limits.