简体   繁体   English

获取 Facebook 图 API JSON 值

[英]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.我有一个简单的 PHP 代码,它通过 JSON 获取 Facebook 图形 API 的值(某个粉丝专页的点赞总数)。 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.它适用于绝大多数粉丝页面,但对于 3 个(我跟踪的 17 个粉丝页面中的)它根本无法读取和打印该值。

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:这是为 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.但是,如果我只是将 ID 更改为 160788560626000 或 167134649991034,那么相同的代码将不再起作用,并且不会打印任何内容。

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.我不知道这些页面是什么或它们在 Facebook 上的 URL 是什么,所以我无法验证这一点,但是,您的页面可能没有发布(可能不太可能),或者(可能更有可能)是年龄受限制的。 The Facebook graph doesn't let you access a page's information if it is restricted by age.如果页面受年龄限制,Facebook 图表不允许您访问页面信息。 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.不幸的是,即使您传递了 Facebook 应用程序访问令牌,甚至是 18 岁以上用户的访问令牌,情况也是如此。这非常愚蠢,并且必须至少有 5 个关于 bugs.developers.facebook 的错误报告。 com 为它。

Update 6/13/2011 2011 年 6 月 13 日更新
Recently, Facebook made an update that requires you to use a valid user access token to pull information about a page.最近,Facebook 进行了更新,要求您使用有效的用户访问令牌来提取有关页面的信息。 This is a horrendous change, imo, but it is what it is.这是一个可怕的变化,imo,但它就是这样。 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.如果没有有效的用户访问令牌,您将从不是基本信息(根、 http://graph.facebook.com/<page_id> )或photosalbums端点的每个端点收到falseerror响应。

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.因此,如果您请求任何其他端点( postsfeedvideosstatuses等),您需要确保提供有效的用户访问令牌来访问它们。

If I'm right then is the result an array and not a class so try something like $graph["likes"]如果我是对的,那么结果是一个数组而不是一个 class 所以尝试类似 $graph["likes"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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