Search code examples
facebookfacebook-graph-apifacebook-like

List facebook likes underneath every url


I was trying to find the best way to count number of likes for a facebook page url and after googling a lot and playing with the code, i have a code like the one given below. It outputs the likes, name of the page and then the link. I wish to know: 1. How can i use the html tag to convert the link into hyperlink so that I can have something like "Click here to visit" 2. How can monitor performance of 25+ fb_id on an hourly basis with a sorted order (descending) on likes

<?php
$fb_id = '36922302396';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
printf("%s %s %s", $result->likes, $result->name, $result->link);
?>

Edited code as per solution provided

<?php
$fb_id = '36922302396';
$pic = 'https://www.facebook.com/' . urlencode($fb_id) . '/picture?type=square';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
echo $result->likes , " " , $result->name , " " , "<a target='_blank' href=\"" . $result->link . "\" ><img src = $pic></a>";
?>

Thanks


Solution

  • For 1)

    How about

    <?php
    $fb_id = '36922302396';
    $url = 'https://graph.facebook.com/' . urlencode($fb_id);
    $result = json_decode( file_get_contents($url) );
    //printf("%s %s %s", $result->likes, $result->name, $result->link);
    echo "<a href=\"" . $result->link . "\">Link</a>";
    ?>
    

    You can also use

    <a href="https://www.facebook.com/<?php $fb_id ?>">Link</a>
    

    For 2)

    If you know the Page's IDs, then you can just concatenate them to the following :

    GET /?ids=40796308305,339150749455906&fields=id,name,likes,talking_about_count
    

    The result looks like the following

    {
      "40796308305": {
        "id": "40796308305", 
        "name": "Coca-Cola", 
        "likes": 88365218, 
        "talking_about_count": 635936
      }, 
      "339150749455906": {
        "id": "339150749455906", 
        "name": "Pepsi", 
        "likes": 33321925, 
        "talking_about_count": 208761
      }
    }
    

    For hourly stats, you need to setup your script via cron etc. and write the results to a database.