Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...