I have some PHP that will grab the like and share counts for my page. (The url is just an example, of course.)
$url='http://mashable.com/2011/03/25/internet-music-piracy-study/';
$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);
curl_close($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"&g开发者_如何学Got;<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.
From PHP manual:
Returns TRUE on success or FALSE on failure.
However, if the CURLOPT_RETURNTRANSFER option is set,
it will return the result on success, FALSE on failure.
So, check for FALSE using === and !== operators, and also check for zero, not 1 if you're interested in zero.
You can also use var_dump to see what $fbcount really contains.
Try looking at the contents of $fbcount
to see what it contains. if it isn't a number, then your conditional statement won't work.
var_dump($fbcount);
Try this right after curl_close($ch);
It may contain an error, which you can use to debug your curl operation.
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">
<link_stat>
<total_count>213</total_count>
</link_stat>
</fql_query_response>
精彩评论