Last week I posted a photo on my photo blog and on Flickr of my hops starting to grow. Once uploaded, I went to FaceBook and posted a link to the photo there. A few people commented in different places and it got me thinking about how I receive comments.
My photo blog doesn’t receive too many comments to begin with, so when I get one I get excited about it. One of my friends posted her comment on my FaceBook wall, so I started to think about how I could pull those comments into one place.
FaceBook APIs
FaceBook has a few different APIs; there is the one for developing applications within their site. One for developers who would like to use FaceBook’s authentication system, and finally, there is one for pulling data off of FaceBook on your site: FaceBook PHP5 Client library.
Setup Code:
<?php
1: $api_key = ‘xxxxxx’;
$secret = ‘xxxxxx’;
include_once ‘../../assets/php/facebook/facebook.php’;
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
?>
Grab the Message’s Comments:
<?php
$facebookComments = $facebook->api_client->stream_getComments(" xxxx_ xxxxxx"); //*
foreach ($facebookComments as $fb){
$user_details = $facebook->api_client->users_getInfo($fb["fromid"], ‘last_name, first_name’);
echo "<hr>";
echo "<p>from: " . $user_details[0]["first_name"] . " " . $user_details[0]["last_name"] . "</p>;
echo "<p>date: " . date("Y-m-d H:i:s", $fb["time"]+64800) . "</p>;
echo "<p>comment: " . $fb["text"] . "</p>;
}
<?php
Easy right?
I was surprised how quickly I was able to mock up some code. The stream_getComments command takes my user ID number and the comment ID number and returns an array with the following items:
- Fromid = users ID#; *I have a second query to grab the person’s name based on their ID
- Time = Unix timestamp format. I added 64800 second to the time so it would be converted to Mountain Standard Time.
- Text = The text of the Comment
- Id = usernameID_postID_commentID
The Problem:
About an hour later I logged off of FaceBook and the whole thing broke. What I didn’t realize was that the FaceBook library was using existing session data from FaceBook. This makes since that I would have to authenticate in since my FaceBook settings are private.
I set the project aside and came back to it the next day. I came to the conclusion that rather than let the comments site on FaceBook (like I do with Flickr), I will pull the comments down to my server. This will help with two things:
- When contacting any API there is usually a lag and since I am already pulling comments from my SQL database and Flickr’s API, it would take longer for the blog to load.
- I’m not sure how long I will be able to access those comments on FaceBook. I would hope in two years from now if I wanted to pull in those comments they would still exist; I’d rather have the comments where I can access them quickly.
Tags: api, facebook, photo blog, php







This article was published on Wednesday, April 21st, 2010 at 10:30 am. View other posts in the Code category.
If you found this article helpful please share it. If you have questions please ask them in the Comments section.