Alexandre
Alexandre

Reputation: 2050

Xpath and facebook api

I try to parse a result from facebook API which returns this XML:

<links_getStats_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
  <link_stat>
    <url>http://www.facebook.com</url>
    <normalized_url>http://www.facebook.com/</normalized_url>
    <share_count>1624678</share_count>
    <like_count>928267</like_count>
    <comment_count>150197</comment_count>
    <total_count>2703142</total_count>
    <click_count>949</click_count>
    <comments_fbid>405613579725</comments_fbid>
    <commentsbox_count>2328</commentsbox_count>
  </link_stat>
</links_getStats_response>

I want to get the number of Facebook likes.

Here is my code:

$apiFacebook="http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.facebook.com";
$parse = simplexml_load_file($apiFacebook);
$count = $parse->xpath("/links_getStats_response/link_stat/like_count");
print_r($count);

Is the problem with the path?

Upvotes: 2

Views: 739

Answers (1)

Jens Erat
Jens Erat

Reputation: 38732

There are namespaces in your result. Either define and use them or use wildcard for namespace instead:

/*:links_getStats_response/*:link_stat/*:like_count

To register and use namespaces do:

$parse = simplexml_load_file($apiFacebook);
$parse->registerXPathNamespace('fb', 'http://api.facebook.com/1.0/');
$count = $parse->xpath("/fb:links_getStats_response/fb:link_stat/fb:like_count");

The string "fb" can be chosen arbitrarily.

Upvotes: 1

Related Questions