#facebook #php

複数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

朴 起煥

中国育ち、東京暮らし。読んだり書いたり、時には考え込んだり。親になってから5年ほど、ブログ(とその他もろもろ)は完全停止していたけど、気づけばまた書いている。ここにはそんな寄り道の記録を書いています。結論が出ない話も多いけど、それもまたよし。

その頃、英語で書いたもの

TextMate Error: Exception raised during posting of notification

2011年6月4日