-1

PHP apiを使用して、ユーザーのすべての写真でFacebookのいいねの総数を取得するにはどうすればよいですか?

4

2 に答える 2

1

私はこのようにアプローチします:

<?php
 // the url to check how many likes
$url = 'url of image';
// build the facebook query
$fburl = "http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=atom";
// grab the atom dump via facebook api url call above
$ch = curl_init($fburl); // url for page
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$atom_data = curl_exec($ch);
// it returns something like this:
/* <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true"> 
<link_stat> 
 <like_count>9</like_count> 
</link_stat> 
</fql_query_response>  */  
preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1]; 
echo "The image has ".$like_count." likes on facebook";



// OPTION 2 >>> keeping it to a 1 liner:
$data = json_decode(file_get_contents("http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=json"));
echo "The image has " . $data[0]->like_count . " likes on facebook";

 ?>
于 2013-01-10T11:45:53.800 に答える
0

これはphoto、FQLテーブルをクエリして、like_info構造体、特にlike_countを探すことで可能になると思います。したがって、以下のクエリのようなものが機能するはずです。

SELECT object_id, album_object_id, like_info.like_count FROM photo WHERE album_object_id IN (
    SELECT object_id FROM album WHERE owner = me()
)

これは次のようなものを返します:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [object_id] => XXX
                    [album_object_id] => YYY
                    [like_info] => Array
                        (
                            [like_count] => 7
                        )

                )

            [1] => Array
                (
                    [object_id] => XXX
                    [album_object_id] => YYY
                    [like_info] => Array
                        (
                            [like_count] => 9
                        )

                )

            [2] => Array
                (
                    [object_id] => XXX
                    [album_object_id] => YYY
                    [like_info] => Array
                        (
                            [like_count] => 1
                        )

                )
        )
)

ここから、いいねの数が最も多い写真を取得するのは簡単なループになります。


更新:@cpilkoの追加に基づいて、これは機能するはずです:

SELECT object_id, album_object_id, like_info.like_count FROM photo WHERE album_object_id IN (
    SELECT object_id FROM album WHERE owner = me()
) ORDER BY like_info.like_count DESC LIMIT 1
于 2013-01-10T13:43:25.400 に答える