List facebook likes underneath every url

佐手、 提交于 2019-12-12 06:53:55

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25837374/list-facebook-likes-underneath-every-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!