简体   繁体   中英

Fetching Facebook graph API JSON value

I have a simple PHP code that fetches Facebook graph API's value (total number of likes of a certain fanpage) via JSON. It works perfectly fine for the vast majority of fan pages but for 3 (out of 17 fan pages that I track) it simply can't read and print that value.

I don't understand where's the problem since the data I want to fetch is publicly available and the same code is applied for all pages.

Here's the code that prints correct value for a fan page with ID 214014840762:

<?php    
$currsiteurl = 'http://graph.facebook.com/214014840762';  
$graph = json_decode(file_get_contents($currsiteurl));  
$currnofans = $graph->likes;  
echo "Number of likes: ".$currnofans; // prints out 107936    
?>

But if I simply change the ID to 160788560626000 or 167134649991034 the same code doesn't work anymore and it doesn't print anything.

Thanks in advance!

I don't know what these pages are or what their URLs on Facebook are, so I can't verify this but, it's possible that your page is either not published (probably not that likely), or (probably more likely) is age restricted. The Facebook graph doesn't let you access a page's information if it is restricted by age. Unfortunately, this is even true if you pass a Facebook application access token, or even an access token for a user who is over 18. It's pretty stupid, and there have got to be at least 5 bug reports on bugs.developers.facebook.com for it.

Update 6/13/2011
Recently, Facebook made an update that requires you to use a valid user access token to pull information about a page. This is a horrendous change, imo, but it is what it is. Without a valid user access token, you will get a false or error response from every endpoint that isn't the basic info (root, http://graph.facebook.com/<page_id> ) or photos , or albums endpoints.

So, if you're requested any of the other endpoints ( posts , feed , videos , statuses , etc) you'll need to make sure you provide a valid user access token to access them.

If I'm right then is the result an array and not a class so try something like $graph["likes"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM