Return to Snippet

Revision: 29014
at July 19, 2010 20:49 by dtbaker


Updated Code
<?php
// the url to check how many likes
$url = 'http://www.likewizard.com/like-67';
// 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>  */
// grab the like count out, i hate dom parsing, so just use regex:

preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1]; 
echo "The URL $url 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 URL $url has " . $data[0]->like_count . " likes on facebook";

?>

Revision: 29013
at July 19, 2010 20:48 by dtbaker


Updated Code
<?php
// the url to check how many likes
$url = 'http://www.likewizard.com/like-67';
// 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>  */
// grab the like count out, i had dom parsing, so just use regex:

preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1]; 
echo "The URL $url 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 URL $url has " . $data[0]->like_count . " likes on facebook";

?>

Revision: 29012
at July 19, 2010 20:41 by dtbaker


Initial Code
<?php
// the url to check how many likes
$url = 'http://www.likewizard.com/like-67';
// 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>  */
// grab the like count out, i had dom parsing, so just use regex:

preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1]; 
echo "The URL $url has $like_count likes on facebook";

?>

Initial URL
http://codecanyon.net/forums/thread/help-atom-feed-to-database/28994

Initial Description
Use the facebook api to grab how many \\\'likes\\\' a url has had.

Initial Title
Get the number of Facebook Likes using PHP

Initial Tags
facebook

Initial Language
PHP