Grabbing FaceBook Comments – More Primitive Programming

Banksy's caveman

Functional is a kind way to describe this . . . but it’s hard to argue with computers doing tedious work that was previously done by hand.

The goal here was to automate the collection of the comments on 300 or so posts from the CDC’s Facebook page so they could be analyzed. Ebola is in the mix which leads to some comments that may very well rival YouTube comments in terms of causing me sadness. We did end up with around 34,000 comments which would have been hand copied in the past. So some karma balance was achieved.

wTake all this with a grain or two of salt. It works but may very well be illegal in some states or do things in a way that would make real programmers weep. Consider my programming skills to be like a man trying to swat flies with a broomstick- lots of furious action, panting, cursing, and every so often I will kill a fly and celebrate wildly.1

The Facebook

This was my first experience trying to do anything with Facebook. The hardest part here for me was just getting the stupid access token needed to get at the JSON feeds. I eventually found the secretly named Facebook Login Example. Part of my issue was that I thought I could skip the user level login and just use the app id/secret stuff like I’d do in Instagram (which is the main API I’ve messed with previously). It may be that someone can do that but I failed to do it in lots of ways. I eventually bowed to my own limits and went with the login option.

To make this work, you set up that login file and in my case I entered by app id/secret stuff in there directly. I’m just running this locally on a virtual server so I don’t really care about exposing it any ways. If you put this somewhere, you’d want to think harder about this and there’s likely a proper way to do it that I missed.


//wherever you stuck the FB SDK PHP stuff
require_once 'vendor/facebook/php-sdk-v4/src/Facebook/autoload.php';

//I may have overloaded here but if you wanted perfection, you are in the wrong place
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

$fb = new Facebook\Facebook([
  'app_id' => 'xxxYOURMAGICIDxxx',
  'app_secret' => 'xxxYOURSUPERSECRETSECRETxxx',
  'default_graph_version' => 'v2.2',

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('', $permissions);//send them to the place where the stuff happens

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

So now, you’re logged in. FB is kind enough to make the landing page so I used that initially. It gave me a warm comfortable feeling to see that the token was actually generated.2 I did a quick manual test by tacking it on the end of a URL by hand and presto- I could get the comments for a particular post via JSON.

The URL I used for testing looks like

Now that I knew I could get the JSON, it became a pretty close match to what I did a few weeks ago for Instagram. The one element I added here was the foreach element. I tied it in to the array of article IDs and that makes it loop through the entire set. I also through in a break in the loop to see if it’d kill the loop if there was no next option present. It may have worked. I’m not sure.

Feel free to wander around in the comments here.

//small sample of the array consisting of the article IDs
$articlearray = array(

foreach ($articlearray as &$articleid) {
$cdcLink = ''.$articleid.'/comments/?access_token='.($accessToken->getvalue());
//echo '<a href="'.$cdcLink.'">link</a>';
$search ='ebola';
$json = file_get_contents($cdcLink);
$obj = json_decode($json);

			$list = array();
			foreach ($obj->data as $comment) {
			 	$username = $comment->from->name;
                $userid = $comment->from->id;
                $message = $comment->message;
                $created = $comment->created_time;
                $commentid = $comment->id;
				array_push($list, $username . '?' . $userid . '?' . $message . '?' . $created . '?' . $commentid) ;
			$file = fopen($search .'fbdata.csv',"a+");

			foreach ($list as $line)


//starts the looping for comments above those initially listed

 for ($x = 0; $x <= 70; $x++) {
				$url = $cdcLink;	
				$next = $obj->paging->next;	
				$url = $next;
				 if ($next == FALSE) {
       			 break;    /* You could also write 'break 1;' here. */
				$json = file_get_contents($url);	
				$obj = json_decode($json);
//				echo $next . '<br>'; - was for testing url returns 
				$list = array();
				foreach ($obj->data as $comment) {
			 	$username = $comment->from->name;
                $userid = $comment->from->id;
                $message = $comment->message;
                $created = $comment->created_time;
                $commentid = $comment->id;
				array_push($list, $username . '?' . $userid . '?' . $message . '?' . $created . '?' . $commentid) ;
			$file = fopen($search .'fbdata.csv',"a+");

			foreach ($list as $line)



1 I am killing more flies, more frequently. So I have that going for me.

2 At least 90% of my programming is just making sure I achieved what I thought I did in each step- so there’s lots of echo/dump/console logging.

Comments on this post

  1. sam said on October 19, 2015 at 11:34 pm

    tthanks tom im not a a programmer how can i use this code ?

    i use the previous code throuh bookmarklet

    • Tom Woodward said on October 20, 2015 at 3:17 pm

      All this stuff is pretty new to me as well. I don’t really qualify as a programmer.

      I run it on a Scotch box install on my computer. That lets you avoid having to put it on a server somewhere.

      What exactly are you trying to do? I might be able to give some advice if I had some specifics. If you’re going to use the code above you will need a FB API key.

  2. sam said on October 28, 2015 at 7:31 pm

    sometimes i need to see full comments on some posts if i use your previous bookmarklet code i can expand not only one post but many if i scroll down is it possible within this php code to expand many posts at the same time ?

    can you help me through teamviewer i tried to instal scotch box but i failed

    • Tom Woodward said on October 29, 2015 at 8:53 pm

      The PHP actually downloads all of the comments and puts them in a CSV file.

      If you just want the plugin, send me a link to something on FB that I can see and I’ll see if I can make the bookmarklet work with it.

  3. sam said on October 30, 2015 at 4:01 am

    please read the last comment

    can you make a bookmarklet from this code ?

    if you want to try your code on fb

    i hope you find solution to (see more)

  4. Tom Woodward said on October 30, 2015 at 2:48 pm

    What’s your browser/OS? It works for me on a Mac (10.10.5) in Chrome (46.0.2490.80 (64-bit)).

  5. sam said on October 30, 2015 at 4:26 pm

    • Tom Woodward said on November 1, 2015 at 9:38 am

      This addresses the first issue. The second one would be a link to a new page rather than expanding anything so it’s a different thing entirely. I’m not sure about the third piece but I think that’s addressed as well.

  6. sam said on November 1, 2015 at 1:01 pm

    thanks tom first issue solved now . about second and third issue is it possible to make Facebook show posts in the same page without clicking on Continue reading and see more in any page i want to see full posts when i scroll down in any Facebook page or account

    • Tom Woodward said on November 1, 2015 at 9:54 pm

      OK- got the second one but the final one that takes you to another page can’t be solved in this manner. Sorry. Update is here.

  7. sam said on November 9, 2015 at 3:12 am

    1.thanks tom if you type in facebook posts liked by my or by any of your friend you cant see any comment in any posts unless you click on comments on each posts is it possible to make Facebook show all comments in all posts ?

    2.about (continue reading ) so you mean its impossible to do it in any programming method ?

    3. this code not solved see more in posts it solved in comments not in posts i hope you can find solution for it

    4. in some posts with 1000 comments page will be Page unresponsive mikake edit his code maybe it will solve this issue please update
    your code