php - An answer to the 'how to get Facebook shares and likes of some URL ?' questions -
i building website need retrieve facebook shares , likes of numerous links , urls different sites.
the problem is, urls impossible wanted. example, when data links http://www.example.com/?a=1&b=2&c=3
wrong data http://www.example.com/?a=1
, rest of url (&b=2&c=3
) ignored facebook.
here @ stackoverflow, lot of people looking answer , many questions unanswered. so, once did right, i'm tell how did it.
p.s. : works non-facebook urls. if you're looking shares , likes counts of internal facebook link (image, video ...), won't work you. i'll using php answer question.
to likes , shares counts, use fql instead of graph api (even though using graph api send query).
but not enough : able so, had call rawurlencode()
function on url want data about. otherwise, i'll keep getting errors.
so, likes()
, function using have counts :
function likes($url) { $url = rawurlencode($url); $json_string = file_get_contents("http://graph.facebook.com/fql?format=json&q=select%20share_count,%20like_count%20from%20link_stat%20where%20url='$url'"); $json = json_decode($json_string, true); if (key_exists("data", $json)) { if (is_array($json["data"])) { if (array_key_exists("0", $json["data"])) { return intval($json["data"]["0"]["share_count"]) + intval($json["data"]["0"]["like_count"]); } else { echo "error : '0' no key<br/>"; return 0; } } else { echo "error : data no table <br/>"; return 0; } } else { echo "error : no data key <br/>"; return 0; } }
i hope someday :)
Comments
Post a Comment