複数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
        )
)
*/
Share the post
Qihuan Piao

朴 起煥

東京で働いている「外人歴」9年のソフトウェア「ライター」。いつの間にか納豆が食えるようになり、これで日本に慣れきったと思いきやまだまだ驚きが続いてる。読んだり書いたりするのが好きで、自身の経験や本から得た「何か」をここに書き出してる。最近古本屋にハマってる。