使用 FQL 从 Facebook API 仅检索即将到来的生日

发布于 2024-12-13 14:50:50 字数 1222 浏览 0 评论 0原文

使用有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-12-20 14:50:50

虽然晚了几个月,但也许可以帮助其他面临同样问题的人。基本上,FQL IN 运算符不会检查该值是否在给定范围内,而是检查给定值是否与提供的任何不同值相匹配。在您的示例中,如果值是“09”和“10”,则返回 9 月和 10 月的所有生日,因为这些生日月份与提供的月份数字匹配。

您可以做的是使用 PHP 或您正在使用的任何其他语言计算以下值,并在 FQL 中使用它们(今天是 3 月 17 日,假设我们希望 4 月 17 日是结束日期):

  • 该月的今天 (17)、
  • 今天一年中的月份 (03)
  • 结束日期 月份中的日期 (17)
  • 结束日期 一年中的月份 (04)

确保分别使用 dd 和 MM 格式化日期和月份(如果是个位数,则用零填充)。那么您的 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, 2) = '03'
      AND substr(birthday_date, 3, 5) >= '17')
      OR (substr(birthday_date, 0, 2) = '04'
      AND substr(birthday_date, 3, 5) < '17'))
      ORDER BY birthday_date

这将返回生日在 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):

  • Today's day of the month (17),
  • Today's month of the year (03)
  • End date day of the month (17)
  • End date month of the year (04)

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:

      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, 2) = '03'
      AND substr(birthday_date, 3, 5) >= '17')
      OR (substr(birthday_date, 0, 2) = '04'
      AND substr(birthday_date, 3, 5) < '17'))
      ORDER BY birthday_date

This will then return the friends who's birthday falls between 17 March and 17 April.

故乡的云 2024-12-20 14:50:50
AND created_time > strtotime('now') AND created_time < $endofnext

参考示例@ http://www.php.net/manual/en /function.strtotime.php#97025

使用“now”将返回当前白天。


无论是哪一个月,该函数都会返回下个月的最后一天。

<?php 
/* $now can be set to any strtotime(), yesterday, last month, last year etc; */
$now = "now"; 
function EndOfNext($now){
    function lastDayOfMonth($month = '', $year = '') 
    { 
       if (empty($month)) { 
          $month = date('m'); 
       } 
       if (empty($year)) { 
          $year = date('Y'); 
       } 
       $result = strtotime("{$year}-{$month}-01"); 
       $result = strtotime('-1 second', strtotime('+1 month', $result)); 
       return date('m/d/y', $result); 
    } 
    function addRealMonth($timeStamp) 
    { 
        $tempMonth = date('m', $timeStamp); 
        $tempYear  = date('Y', $timeStamp); 
        if($tempMonth == "12") 
        { 
            $tempMonth = 1; 
            $tempYear++; 
        } 
        else 
            $tempMonth++; 

        $newDate = lastDayOfMonth($tempMonth, $tempYear); 
        return strtotime($newDate); 
    }
    $newChargeDate = strtotime($now); 
    $newChargeDate = addRealMonth($newChargeDate); 
    return date("m/d/y", $newChargeDate); 
}
$endofnext = EndOfNext($now);
echo $endofnext;
?>
AND created_time > strtotime('now') AND created_time < $endofnext

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.

<?php 
/* $now can be set to any strtotime(), yesterday, last month, last year etc; */
$now = "now"; 
function EndOfNext($now){
    function lastDayOfMonth($month = '', $year = '') 
    { 
       if (empty($month)) { 
          $month = date('m'); 
       } 
       if (empty($year)) { 
          $year = date('Y'); 
       } 
       $result = strtotime("{$year}-{$month}-01"); 
       $result = strtotime('-1 second', strtotime('+1 month', $result)); 
       return date('m/d/y', $result); 
    } 
    function addRealMonth($timeStamp) 
    { 
        $tempMonth = date('m', $timeStamp); 
        $tempYear  = date('Y', $timeStamp); 
        if($tempMonth == "12") 
        { 
            $tempMonth = 1; 
            $tempYear++; 
        } 
        else 
            $tempMonth++; 

        $newDate = lastDayOfMonth($tempMonth, $tempYear); 
        return strtotime($newDate); 
    }
    $newChargeDate = strtotime($now); 
    $newChargeDate = addRealMonth($newChargeDate); 
    return date("m/d/y", $newChargeDate); 
}
$endofnext = EndOfNext($now);
echo $endofnext;
?>
自此以后,行同陌路 2024-12-20 14:50:50

您还可以让 FQL 为您做一些工作,如下所示:

SELECT uid, name, birthday_date, strtotime(substr(birthday_date, 0, 5)) > now()
FROM user
WHERE uid in (SELECT uid2 FROM #freunde)
AND birthday_date <> ''
AND strpos(birthday_date,'06') = 0
ORDER BY substr(birthday_date, 0, 5)

返回一个布尔类型“anon”字段,告诉您是否必须将此结果包含在显示列表中(true),或者该用户的生日是否已存在于显示列表中。过去(假)。
不幸的是,我无法让这个命题作为 WHERE 子句工作 - 继续出现错误,但很想知道是否有人比我幸运。

  "error": {
    "message": "Call to a member function on a non-object", 
    "type": "BadMethodCallException"
  }

You can also let FQL do some job for you, like in the following:

SELECT uid, name, birthday_date, strtotime(substr(birthday_date, 0, 5)) > now()
FROM user
WHERE uid in (SELECT uid2 FROM #freunde)
AND birthday_date <> ''
AND strpos(birthday_date,'06') = 0
ORDER BY substr(birthday_date, 0, 5)

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.

  "error": {
    "message": "Call to a member function on a non-object", 
    "type": "BadMethodCallException"
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文