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

时间:2011-04-28 17:24:47

标签: php facebook facebook-graph-api facebook-fql

我正在开发一个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//////////////////////**
}

&GT?;

1 个答案:

答案 0 :(得分:1)

你的问题有点难以理解;但从我的理解 - 你在特定日期找到朋友的生日时遇到了问题。

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

另一种选择:( 已编辑更完整。现在几乎完全复制和粘贴。

<?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
            )

    )
)