使用 FQL 从 Facebook API 仅检索即将到来的生日
使用有关 FQL 的信息和用户表 来自 Facebook 的开发人员文档,我想出了以下代码。
//build fql string
$fql = "SELECT name, birthday_date FROM user
WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me())
AND strlen(birthday_date) != 0 AND substr(birthday_date,0,3)
IN ('{$currentMonth}/', '{$nextMonth}/')";
$fql.=$orderBy;
$fql.=" LIMIT " . $limit;
//query facebook
$params = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$birthdays = $this->facebook_obj->api($params);
$currentMonth
和 $nextMonth
分别设置为当前月份和下个月的字符串表示形式,采用 mm 格式。 (例如“09”、“10”等)
这是“工作”,但假设今天是 11 月 29 日,我有 20 个朋友的生日在 11 月,并且 $limit
设置为 10。在这种情况下,它返回的 10 个生日很有可能都在 11 月 29 日之前。
我真正想做的是从今天开始,本月和下个月即将到来的生日。我怎样才能修改这个查询来完成这个任务?
谢谢!
**编辑,请参阅肖恩和我之间的评论线程以获取可能的替代解决方案。尚未接受答案,因为没有人直接解决原始代码/问题。
Using the information on FQL and the user table from Facebook's Developer's docs, I've come up with the following code.
//build fql string
$fql = "SELECT name, birthday_date FROM user
WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me())
AND strlen(birthday_date) != 0 AND substr(birthday_date,0,3)
IN ('{$currentMonth}/', '{$nextMonth}/')";
$fql.=$orderBy;
$fql.=" LIMIT " . $limit;
//query facebook
$params = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$birthdays = $this->facebook_obj->api($params);
$currentMonth
and $nextMonth
are set to string representations of the current month and next month respectively, in mm format. (eg "09", "10" etc.)
This is "working", but let's say that today is November 29th, I have 20 friends with birthday's in November and $limit
is set to 10. In this case, there is a very good chance that the 10 birthdays it returns will all be before November 29th.
What I would really like to do is get upcoming birthdays from today's day out, for this month and next. How could I modify this query to accomplish that?
Thanks!
** Edit, see comment thread between Shawn and I for possible alternative solutions. No answer accepted yet as none directly addresses original code / question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然晚了几个月,但也许可以帮助其他面临同样问题的人。基本上,FQL
IN
运算符不会检查该值是否在给定范围内,而是检查给定值是否与提供的任何不同值相匹配。在您的示例中,如果值是“09”和“10”,则返回 9 月和 10 月的所有生日,因为这些生日月份与提供的月份数字匹配。您可以做的是使用 PHP 或您正在使用的任何其他语言计算以下值,并在 FQL 中使用它们(今天是 3 月 17 日,假设我们希望 4 月 17 日是结束日期):
确保分别使用 dd 和 MM 格式化日期和月份(如果是个位数,则用零填充)。那么您的 FQL 将如下所示:
这将返回生日在 3 月 17 日到 4 月 17 日之间的朋友。
This is some months late but maybe helps other people facing the same problem. Basically, the FQL
IN
operator does not check whether the value is in a given range, but whether the given value matches any of the distinct values that are provided. In your example if '09' and '10' are the values, then all birthdays in September and October are returned since those birthday months match the provided month numbers.What you can do is calculate the following values using PHP or whatever other language you are using and use them in your FQL (today is 17 March and say we want 17 April is end date):
Make sure days and months are formatted using dd and MM respectively (padded with zero in case of single digits). Then your FQL would look like this:
This will then return the friends who's birthday falls between 17 March and 17 April.
参考示例@ http://www.php.net/manual/en /function.strtotime.php#97025
使用“now”将返回当前白天。
无论是哪一个月,该函数都会返回下个月的最后一天。
refer to samples @ http://www.php.net/manual/en/function.strtotime.php#97025
using "now" would return the current daytime.
This function will return the last day of next month no matter what month it is.
您还可以让 FQL 为您做一些工作,如下所示:
返回一个布尔类型“anon”字段,告诉您是否必须将此结果包含在显示列表中(true),或者该用户的生日是否已存在于显示列表中。过去(假)。
不幸的是,我无法让这个命题作为 WHERE 子句工作 - 继续出现错误,但很想知道是否有人比我幸运。
You can also let FQL do some job for you, like in the following:
returning a boolean type "anon" field telling if you have to include this result in the display list (true) or if the birthday for this user lies already in the past (false).
Unfortunately I wasn't able to have this proposition work as a WHERE clause - keep getting the error under, but would love to know if someone was luckier than I.