I have some PHP that will grab the like and share counts for my page. (The url is just an example, of course.)

$furl = "https://api.facebook.com/method/fql.query?query=select%20total_count%20from%20link_stat%20where%20url=%27".$url."%27&format=xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $furl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$fbcount = curl_exec($ch);

I then want to use a generic facebook icon in my page if the number I get back is 0. Otherwise, I get the number. Basically, I don't want a big fat zero if people don't like a page.

<div class="burst-fb"><span><?php if ($fbcount < 1) {echo 'logo stuff';} else {echo $fbcount;}?></span></div>

Problem is, well, it's not working. I know there must be something basic I'm overlooking here.


Have you echo'd out the $fbcount variable to see what its return value is?

Written by Prisoner

Which is the error? Did you install curl module for php?

Written by Javi R

Accepted Answer

The response from the facebook api (and therefor what is in $fbcount) is an XML string, not a number. You need to parse the response. Sorry ;). Have a look at simpleXML. Edit: Copy and paste the url (https://api.facebook.com/method/fql.query?query=select%20total_count%20from%20link_stat%20where%20url=%27http://mashable.com/2011/03/25/internet-music-piracy-study/%27&format=xml) into your browser to see what you'll get. The following should appear in your browser:

<fql_query_response list="true">
Written by user578368
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki