如何在日历中显示我的 Facebook 好友的照片

发布于 2024-11-03 14:53:03 字数 2959 浏览 0 评论 0原文

我正在开发一个 Facebook 应用程序,我想在其中显示我朋友的照片和他的出生日期。例如,今天是 4 月 26 日;如果我朋友的生日是今天,他/她的照片应该显示在 26 日的日历槽中,依此类推。

到目前为止,这是我的查询:

$dt = "April 12";

$fql1 = "SELECT uid,pic_big,pic,pic_small,name 
    FROM user 
    WHERE birthday='".$dt."' AND 
        uid IN (SELECT uid2 FROM friend WHERE uid1= $user)";

我希望能够处理完整日期(即 1986 年 4 月 12 日)和部分日期日期(4 月 12 日)。

这是我的日历代码

<table width="100%" cellpadding="0" cellspacing="0" height="400"  align="center" style="border:1px solid #cccccc;">
    <tr><td colspan="3" align="center" class="viewlink"><?php echo $msg;?></td></tr>
    <tr >

    <td width="120" colspan="1" >
        <input type="button" value=" < " onClick="goLastMonth(<?php echo $month . ", " . $year; ?>);">
    </td>
    <td colspan="5" width="610" align="center">
        <span class="title"><?php echo $monthName . " " . $year; ?></span><br>
    </td>
    <td width="120" colspan="1" align="right" style="padding-right:2px;">
        <input type="button" value=" > " onClick="goNextMonth(<?php echo $month . ", " . $year; ?>);">
    </td>
</tr>
<tr>
    <th>Sunday</td>
    <th width="45">Mondy</th>
    <th width="89">Tuesday</th>
    <th width="126">Wednesday</th>
    <th width="98">Thursday</th>
    <th width="69">Friday</th>
    <th>Saturday</th>
</tr>
<tr>
<?php
    for($i = 1; $i < $numDays+1; $i++, $counter++){
        $dateToCompare = $month . '-' . $i . '-' . $year;
        $timeStamp = strtotime("$year-$month-$i");
        //echo $timeStamp . '<br/>';
        if($i == 1){
            // Workout when the first day of the month is
            $firstDay = date("w", $timeStamp);
            for($j = 0; $j < $firstDay; $j++, $counter++){
                echo "<td>&nbsp;</td>";
            }
        }
        if($counter % 7 == 0){
            ?>
            </tr><tr>
            <?php
        }

        $pdate=strlen($i);
        if($pdate==1)
        {
            $day="0".$i;
        }
        else
        {
            $day=$i;
        }

        $pmonth=strlen($month);
        if($pmonth==1)
        {
            $mon="0".$month;
        }
        else
        {
            $mon=$month;
        }

    ?>

    <!--right here--><td width="323" <?=hiLightEvt($month,$i,$year);?> valign="top" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#960; text-align:center; padding:10px; margin:0; border:1px dotted #cccccc;" class="viewlink" ><?=$i;?><br />

    <?php
        **/////////////////////////////Here I want to display friends picture whose birthday at relevant date//////////////////////**
}

?>

I'm developing a Facebook app in which I want to display the picture of my friend against his birth date. For example, today is April 26; if any of my friend's birthdays are today, his/her picture should display in the calendar slot for the 26th, etc.

Here's my query so far:

$dt = "April 12";

$fql1 = "SELECT uid,pic_big,pic,pic_small,name 
    FROM user 
    WHERE birthday='".$dt."' AND 
        uid IN (SELECT uid2 FROM friend WHERE uid1= $user)";

I want to be able to handle both full dates (i.e. April 12, 1986) and partial dates (April 12).

This is my Calender code

<table width="100%" cellpadding="0" cellspacing="0" height="400"  align="center" style="border:1px solid #cccccc;">
    <tr><td colspan="3" align="center" class="viewlink"><?php echo $msg;?></td></tr>
    <tr >

    <td width="120" colspan="1" >
        <input type="button" value=" < " onClick="goLastMonth(<?php echo $month . ", " . $year; ?>);">
    </td>
    <td colspan="5" width="610" align="center">
        <span class="title"><?php echo $monthName . " " . $year; ?></span><br>
    </td>
    <td width="120" colspan="1" align="right" style="padding-right:2px;">
        <input type="button" value=" > " onClick="goNextMonth(<?php echo $month . ", " . $year; ?>);">
    </td>
</tr>
<tr>
    <th>Sunday</td>
    <th width="45">Mondy</th>
    <th width="89">Tuesday</th>
    <th width="126">Wednesday</th>
    <th width="98">Thursday</th>
    <th width="69">Friday</th>
    <th>Saturday</th>
</tr>
<tr>
<?php
    for($i = 1; $i < $numDays+1; $i++, $counter++){
        $dateToCompare = $month . '-' . $i . '-' . $year;
        $timeStamp = strtotime("$year-$month-$i");
        //echo $timeStamp . '<br/>';
        if($i == 1){
            // Workout when the first day of the month is
            $firstDay = date("w", $timeStamp);
            for($j = 0; $j < $firstDay; $j++, $counter++){
                echo "<td> </td>";
            }
        }
        if($counter % 7 == 0){
            ?>
            </tr><tr>
            <?php
        }

        $pdate=strlen($i);
        if($pdate==1)
        {
            $day="0".$i;
        }
        else
        {
            $day=$i;
        }

        $pmonth=strlen($month);
        if($pmonth==1)
        {
            $mon="0".$month;
        }
        else
        {
            $mon=$month;
        }

    ?>

    <!--right here--><td width="323" <?=hiLightEvt($month,$i,$year);?> valign="top" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#960; text-align:center; padding:10px; margin:0; border:1px dotted #cccccc;" class="viewlink" ><?=$i;?><br />

    <?php
        **/////////////////////////////Here I want to display friends picture whose birthday at relevant date//////////////////////**
}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨与橙与城 2024-11-10 14:53:03

你的问题有点难以理解;但据我所知 - 您在查找朋友特定日期的生日时遇到问题。

我建议以不同的方式解决这个问题。您似乎正在为日历中的每个日期执行单个 FQL 查询。我认为最好加载用户的所有朋友,然后循环遍历每个朋友并推断日期。根据 Facebook 文档,生日日期格式与用户的区域设置相关,因此您将无法可靠地查询该字段。

另一种选择:(编辑使其更加完整。现在几乎完全可以复制和粘贴。

<?php

$facebook = new Facebook(array(
    'appId' => FB_APP_ID, //put your FB APP ID here
    'secret' => FB_APP_SECRET, //put your FB APP SECRET KEY here
    'cookie' => true
));

$session = $facebook->getSession();

if ($session)
{
    //check to see if we have friends_birthday permission
    $perms = $facebook->api('/me/permissions');
}

 //we do this to see if the user is logged & installed
if (empty($session) || empty($perms['friends_birthday']))
{
    //get url to oauth endpoint for install/login
    $loginUrl = $facebook->getLoginUrl(array(
        //put the URL to this page, relative to the FB canvas
        //(apps.facebook.com) here vvv
        'next' => 'http://apps.facebook.com/path_to_your_app/index.php',
        'req_perms' => 'friends_birthday'
    ));

    //use javascript to redirect. the oauth endpoint cant be loaded in an
    //iframe, so we have to bust out of the iframe and load it in the browser
    //and tell the oauth endpoint to come back to the fb canvas location
    echo "<script>window.top.location='{$loginUrl}';</script>";
    exit;
}

$user = $session['uid']; //load the user id from the session array

$cur_month_birthdays = array();
$cur_month = 4; //april

try {

    $fql1 = "SELECT uid, pic_big, pic,pic_small, name, birthday FROM user "
    .   "WHERE uid IN ( "
    .       "SELECT uid2 FROM friend WHERE uid1 = {$user} "
    .   ")";

    $friends = $facebook->api(array(
        'method'    => 'fql.query',
        'query'     => $fql1
    ));

    //make sure i have some friends...
    if (!empty($friends)) 
    {
        foreach ($friends as $friend)
        {
            //if this friend doesn't have their bday specified, skip them.
            if (empty($friend['birthday']))
            { 
                continue;
            }

            //get unix time for the users' birthday
            $birthday_ts = strtotime($friend['birthday']);

            //if this friends birthday is this month...
            if (date('m', $birthday_ts) == $cur_month)
            {
                //generate a month-day string for the birthdate
                $birthday_str = date('m-d', $birthday_ts);

                //initialize the array of friends with birthdays on this date
                if (empty($cur_month_birthdays[ $birthday_str ]))
                {
                    $cur_month_birthdays[ $birthday_str ] = array();
                }

                $cur_month_birthdays[ $birthday_str ] []= $friend;
            }
        }
    }
}
catch (Exception $e)
{
    //error with facebook
    error_log($e);
}

//output list of days in this month with friends that have birthdays
print_r($cur_month_birthdays); 

上面的代码将获取所有用户的朋友,循环遍历每个朋友,并找到生日位于当前月份(定义为 $cur_month)。然后,您可以循环并构建日历,并每天检查 $cur_month_birthdays 数组以查看是否有任何朋友在当天过生日,并适当地显示它们。

希望有帮助!

** 编辑 **

这是我对上述脚本的输出。我对其进行了编辑,使其设置更加彻底(包括 FB PHP SDK 的初始化和获取用户会话。我假设您至少有该代码。现在您可以看到数组的结构 - 您应该能够轻松地弄清楚如何集成到您的日历中。

Array
(
    [04-04] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Thomas W
                [birthday] => April 4, 1985
            )

    )

    [04-19] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Joel S
                [birthday] => April 19
            )

    )

    [04-29] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Ashley F
                [birthday] => April 29, 1983
            )

            [1] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Megan S
                [birthday] => April 29, 1989
            )

    )
)

Your question is a little hard to understand; but from what I do understand - you're having a problem finding your friends' birthdays for a specific date.

I would suggest going about this problem in a different way. It looks like you're doing a single FQL query for each date in your calendar. I think it'd be better to load all of a users' friends, and loop through each of them and extrapolate the date. According to Facebook's documentation, the birthday date format is relative to the users' locale, so you are not going to be able to reliably query against that field.

An alternative: (edited to be more complete. almost completely copy and pasteable now.)

<?php

$facebook = new Facebook(array(
    'appId' => FB_APP_ID, //put your FB APP ID here
    'secret' => FB_APP_SECRET, //put your FB APP SECRET KEY here
    'cookie' => true
));

$session = $facebook->getSession();

if ($session)
{
    //check to see if we have friends_birthday permission
    $perms = $facebook->api('/me/permissions');
}

 //we do this to see if the user is logged & installed
if (empty($session) || empty($perms['friends_birthday']))
{
    //get url to oauth endpoint for install/login
    $loginUrl = $facebook->getLoginUrl(array(
        //put the URL to this page, relative to the FB canvas
        //(apps.facebook.com) here vvv
        'next' => 'http://apps.facebook.com/path_to_your_app/index.php',
        'req_perms' => 'friends_birthday'
    ));

    //use javascript to redirect. the oauth endpoint cant be loaded in an
    //iframe, so we have to bust out of the iframe and load it in the browser
    //and tell the oauth endpoint to come back to the fb canvas location
    echo "<script>window.top.location='{$loginUrl}';</script>";
    exit;
}

$user = $session['uid']; //load the user id from the session array

$cur_month_birthdays = array();
$cur_month = 4; //april

try {

    $fql1 = "SELECT uid, pic_big, pic,pic_small, name, birthday FROM user "
    .   "WHERE uid IN ( "
    .       "SELECT uid2 FROM friend WHERE uid1 = {$user} "
    .   ")";

    $friends = $facebook->api(array(
        'method'    => 'fql.query',
        'query'     => $fql1
    ));

    //make sure i have some friends...
    if (!empty($friends)) 
    {
        foreach ($friends as $friend)
        {
            //if this friend doesn't have their bday specified, skip them.
            if (empty($friend['birthday']))
            { 
                continue;
            }

            //get unix time for the users' birthday
            $birthday_ts = strtotime($friend['birthday']);

            //if this friends birthday is this month...
            if (date('m', $birthday_ts) == $cur_month)
            {
                //generate a month-day string for the birthdate
                $birthday_str = date('m-d', $birthday_ts);

                //initialize the array of friends with birthdays on this date
                if (empty($cur_month_birthdays[ $birthday_str ]))
                {
                    $cur_month_birthdays[ $birthday_str ] = array();
                }

                $cur_month_birthdays[ $birthday_str ] []= $friend;
            }
        }
    }
}
catch (Exception $e)
{
    //error with facebook
    error_log($e);
}

//output list of days in this month with friends that have birthdays
print_r($cur_month_birthdays); 

The above code will fetch all your users' friends, loop through each, and find the ones with a birthday that is in your current month (defined as $cur_month). You can then loop through and build your calendar, and for each day you check the $cur_month_birthdays array to see if there are any friends that have a birthday on the current day, and display them appropriately.

Hope that helps!

** edit **

This is my output for the above script. I have edited it to be more thorough it's set-up (including initialization of the FB PHP SDK and fetching of the users' session. I had assumed that you had at least that code in place. Now that you can see the structure of the array - you should be able to easily figure out how to integrate into your calendar.

Array
(
    [04-04] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Thomas W
                [birthday] => April 4, 1985
            )

    )

    [04-19] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Joel S
                [birthday] => April 19
            )

    )

    [04-29] => Array
    (
            [0] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Ashley F
                [birthday] => April 29, 1983
            )

            [1] => Array
            (
                [uid] => 123123
                [pic_big] => url-to-pic
                [pic] => url-to-pic
                [pic_small] => url-to-pic
                [name] => Megan S
                [birthday] => April 29, 1989
            )

    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文