Facebook Graph API - 如何获取用户所在国家/地区?
我正在使用他们的 Graph API 开发 Facebook 应用程序。我之前使用已弃用的 REST API 开发了一个应用程序。
我遇到一个问题,因为我无法检索用户的国家/地区,只能检索他们的“位置” - 这只是城市和州/地区的组合格式(例如“德克萨斯州休斯顿”)。
如何使用 Graph API 检索用户的国家/地区?如果这不能直接实现,那么可能有哪些解决方法来实现这一目标?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
https://api.facebook.com/method/fql.query?format=json&query=SELECT%20current_location%20FROM%20user%20WHERE%20uid=4&access_token=
FQL 文档
https://api.facebook.com/method/fql.query?format=json&query=SELECT%20current_location%20FROM%20user%20WHERE%20uid=4&access_token=<ACCESS_TOKEN>
FQL Documentation
我发现的一个解决方案(只是一种解决方法)是使用 GeoNames API - http: //www.geonames.org/export/web-services.html
使用类似的内容:
返回 xml 对象中的结果,其中国家/地区作为数据的一部分。
然而,我对此解决方案并不是特别满意,因为它给我的应用程序增加了不必要的复杂性 - 而且对此免费服务的请求限制为每小时 2000 个,每天 30,000 个。
One solution I have found, which is merely a workaround, is to use the GeoNames API - http://www.geonames.org/export/web-services.html
Using something like:
Returns results in an xml object, with the country as part of the data.
I'm not particularly happy with this as a solution however, as it adds an unnecessary layer of complications on to my application - plus requests to this free services are limited to 2000 per hour and 30,000 per day.
使用php sdk:
然后要求签署请求:
现在 $country 有一个字符串,例如“us”代表美国,“uk”代表英国等。
Use the php sdk:
Then ask for a signed request:
Now $country has a string such as "us" for United States and "uk" for United Kingdom etc.
这是一个好问题。
问题是,任何在 facebook 下运行的应用程序都必须经过 facebook 服务器 - fb 代理,因此您获得的 IP 不是访问者的 IP,而是 facebook 服务器的 IP,而这是没有用的。
我还没有看到任何解决方案,除了从用户的个人资料中获取用户的国家/地区,这只有在用户公开或授权其权限时才可能实现。
这是您可能也想查看的有用链接:
That is a good question.
The problem is that any application run under facebook has to go through facebook servers - fb proxy and because of this the IP you get is not that of visitors but facebook's servers' instead which is of no use.
I have not seen any solution for this yet except for getting user's country from his profile which again is only possible if user had made it public or authorized the permission for it.
Here is useful link you might want to check out there too:
你应该这样尝试:
You should try like this:
使用官方 Facebook Graph API /me?fields=hometown 它将返回您,例如“布拉格,捷克共和国”
我相信你可以用针把字符串炸开','最后一个元素应该是国家
use the official Facebook Graph API /me?fields=hometown it will return you e.g. "Prague, Czech Republic"
I believe you can explode the string by needle ',' and the last element should be the country
对于 iframe 应用程序,FB 服务器不会隐藏 ip,这意味着您可以进行 ip 地址查找
http://phpweby.com /software/ip2country
该脚本对我来说有点错误,但完成了工作。
for iframe apps, then ip is not hidden by FB servers, meaning you can do ip address lookup
http://phpweby.com/software/ip2country
the script was a bit buggy for me but did the job.
根据Facebook的文档,您可以获得2个字母的ISO 3166-1 来自用户
区域设置
的国家/地区代码。FQL 示例:
响应:
According to Facebook's documentation, you can get the 2-letter ISO 3166-1 country code from the user's
locale
.Example FQL:
Response:
您可以使用 FQL 查询按国家/地区查看终生关注者数量(通过对您的 FB 页面的点赞)。使用标记化会话值设置 JSON 查询,并通过 PHP 发送 FQL 查询,其中包含您要定位的页面的 object_ID 和生命周期。选择指标,并将指标设置为 page_fans_country - 并将数据转储到 CSV 文件中。您应该按国家/地区代码(多行)返回终生关注者总数(按喜欢)的列表。设置数据透视表以获取跨多个数据页的聚合索引,并按国家/地区代码排名。以下是 FQL 查询的示例:
SELECT metric, value FROM Insights WHERE object_id=[your page ID gone here] AND metric='page_fans_country' AND end_time=end_time_date('2013-04-28') AND period=period('lifetime' )
You can see the lifetime number of followers (by likes to your FB page) by country using an FQL query. Set up a JSON query using the tokenized session value, and send an FQL query through via PHP with the object_ID of the page you are targeting and period of lifetime. select metric, and set the metric to page_fans_country - and dump the data into a CSV file. You should get back a list of total lifetime followers (by likes) by country code (multiple rows). Setup a pivot table to get an aggregated index across multiple pages of data, and rank by country code. Here's example of the FQL query:
SELECT metric, value FROM insights WHERE object_id=[your page ID goes here] AND metric='page_fans_country' AND end_time=end_time_date('2013-04-28') AND period=period('lifetime')
Valerchik 发布的答案确实是正确的,但万一有人会寻找 PHP 解决方案,那么我最终得到了一些东西像这样:
所有用户属性的列表可以在这里找到:
https://developers.facebook.com/docs/reference/fql/user/< /a>
我希望它能帮助别人:)
Answer posted by Valerchik is the right one indeed, but in case someone would look for PHP solution, then I ended up with something like this:
List of all user's attributes can be found here:
https://developers.facebook.com/docs/reference/fql/user/
I hope it will help someone :)