facebook share count - problem with FQL with different URLs
I have a plugin on my page which allows users to share articles on my site on facebook, but each url is appended with their user id.
What I want to do is count the number of total share regardless of the shareers user id.
However I cannot get the FQL to work as the where parameter will not accept the LIKE function..
the pages will be something like:
page1.php?sid=2 page1.php?sid=34 page1.php?sid=12
But I want to retrieve the total shares for page1.php regardless of the sid.开发者_运维百科
The LIKE function doesnot work in FQL does anyone have any ideas as I am struggling
current code:
https://api.facebook.com/method/fql.que … ge1.php%27
You can use FQL to make your query :
$fql = 'SELECT total_count FROM link_stat WHERE url="http://google.com"';
$json = file_get_contents('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql));
$data = json_decode($json);
echo $data[0]->total_count;
Here, total_count
gives you the number of shares for the link.
If you have several URL to query, you can make all of that in only one query by using OR
:
SELECT url, total_count FROM link_stat WHERE url="..." OR url="..."
Here is an example is you want to get the number of shares for theses 3 URLs :
$urls = array(
"http://www.example.com/page1.php?sid=2",
"http://www.example.com/page1.php?sid=12",
"http://www.example.com/page1.php?sid=34",
);
function wrap($url) {
return 'url="' . $url . '"';
}
$fql = 'SELECT url, total_count FROM link_stat WHERE ';
$fql .= implode(" OR ", array_map("wrap", $urls));
$json = file_get_contents('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql));
$data = json_decode($json);
And $data
is an array of 3 objects with the share number for each URL :
array(4) {
[0]=> object(stdClass)#2 (2) {
["url"]=> string(17) "http://www.example.com/page1.php?sid=2"
["total_count"]=> int(1318598)
}
[1] => ...
[2] => ...
[3] => ...
}
Then you just have to go through the array to make a sum.
Hope that helps !
精彩评论