开发者

How to parse facebook api XML online to retrieve 'like' count?

开发者 https://www.devze.com 2023-02-19 06:23 出处:网络
I\'ve located a method of retrieving an XML file online via api.facebook.com which contains the number of likes a given page has received. For example:

I've located a method of retrieving an XML file online via api.facebook.com which contains the number of likes a given page has received. For example:

https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27http://mashable.com/2011/03/2开发者_Python百科1/google-chrome-icon/%27&format=xml

However, I'm not sure how I can parse that XML in my PHP application to then extract the 'like' number and place it into a variable to use myself.


You can use the built-in simplexml_load_file() but since it doesn't work with HTTPS you can use CURL and simplexml_load_string() function.

<?php
$url = "https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27http://mashable.com/2011/03/21/google-chrome-icon/%27&format=xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
print_r($xml);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消