如何在照片上获得Facebook喜欢的总数?

时间:2013-01-10 11:35:36

标签: php facebook-like facebook-fql

如何使用PHP api获取用户所有照片上的facebook喜欢总数?

2 个答案:

答案 0 :(得分: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";

 ?>

答案 1 :(得分: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