複数URLのFacebookいいね(Like)された数を取得する方法
今はほとんどのサイトがLike(いいね!)ボタンを置いてますが、Likeボタンがどれだけクリックされたか、その数は気になりますね。
Facebook独自のFQL(文法はSQLに似ている)を使って複数URLのLikeされた数が取れます。Facebook公式ドキュメントはこちらにあります。
下記はPHPでのサンプルコードです。
<?php
// see fql docs:
// http://developers.facebook.com/docs/reference/fql/link_stat/
$url_list = array(
'http://example.com/url1.html',
'http://example.com/url2.html',
);
$url_string = '("'. implode('","', $url_list). '")';
// just like sql syntax
$fql = "select url, like_count from link_stat where url in".$url_string;
// remember to encode it
$url = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, true);
$ret = array();
print_r($json);
// result example
/*
Array
(
[0] => Array
(
[url] => http://example.com/url1.html
[like_count] => 10
)
[1] => Array
(
[url] => http://example.com/url2.html
[like_count] => 20
)
)
*/