通过图形API创建和邀请组到事件

时间:2013-10-30 07:53:02

标签: php facebook-graph-api

是否仍无法邀请所有群组成员加入群组活动?我可以通过api创建群组活动,但不会邀请成员参加新活动。如果我通过Facebook网站创建活动,我可以选择“邀请所有群组成员”。我似乎无法找到通过api重现该功能的任何方法。

1 个答案:

答案 0 :(得分:0)

由于没有办法直接通过图形api做到这一点,我最终制作了自己的php w / mysql解决方案。基本上我会列出我所有小组成员的表格,然后检查他们是否被邀请参加我所有小组的活动。这个强制方法的好处是,如果在创建事件后新成员加入,他们将被邀请参加所有未来事件。

facebook不会告诉您每个时间段可以从api邀请多少成员而不会被标记为垃圾邮件

转储组成员的代码:

function UpdateFacebookMemberDB($verbatim = FALSE)
{
    global  $access_token, $groupID;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/members?access_token='.$access_token);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $return = curl_exec($ch);
    curl_close ($ch);
    $decoded = json_decode($return, true);
    $decoded = $decoded['data'];
    Sql_Connect();
    if(!Sql_Query("DELETE FROM `Facebook_User`;"))
    {
        echo "Could not empty the `Facebook_User` table.<br>\n";
        return FALSE;
    }
    foreach ($decoded as $value)
    {
        $query="INSERT INTO `Facebook_User` (`Name`, `FID`) VALUES ('".Sql_CleanInput($value['name'])."', '".Sql_CleanInput($value['id'])."');";
        if(Sql_Query($query))
        {
            if($verbatim)
                echo $value['name']." was added to the database.<br>";
        }
        else if($verbatim)
                echo $value['name']." was <b>NOT</b> added to the database.<br>";
    }

    if(!Sql_Query("DELETE FROM `Facebook_Event`;"))
    {
        echo "Could not empty the `Facebook_Event` table.<br>\n";
        return FALSE;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $return = curl_exec($ch);
    curl_close ($ch);
    $decoded = json_decode($return, true);
    $decoded = $decoded['data'];
    foreach ($decoded as $value)
    {
        $eid = $value['id'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eid.'/invited?access_token='.$access_token);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        $return = curl_exec($ch);
        curl_close ($ch);
        $decod = json_decode($return, true);
        $decod = $decod['data'];
        foreach ($decod as $val)
        {
            $query="INSERT INTO `Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eid."', '".$val['id']."');";
            if(Sql_Query($query))
            {
                if($verbatim)
                echo $val['name']." was added to the database.<br>";
            }
            else if($verbatim)
            echo $val['name']." was <b>NOT</b> added to the database.<br>";
        }
    }
    Sql_Disconnect();
}

检查/邀请成员的代码:

function InviteClubToEvent)
{
    global  $access_token, $groupID;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    $return = curl_exec($ch);
    curl_close ($ch);
    $decoded = json_decode($return, true);
    $decoded = $decoded['data'];
    $invite=0;
    for($x=count($decoded)-1;$x>=0;$x--)
    {
        $eventid = $decoded[$x]['id'];
        Sql_Connect();
        $query="SELECT * from `Facebook_User` ORDER BY `FID` ASC;";
        $qresult=Sql_Query($query);

        $data = array( 'access_token' => $access_token);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        for($i=0;$i<Sql_Num_Rows($qresult);$i++)
        {
            $subquery="SELECT * from Facebook_Event WHERE `UserID` ='".Sql_Result($qresult,$i,"FID")."' AND `EventID` ='".$eventid."'LIMIT 1;";
            if(Sql_Num_Rows(Sql_Query($subquery))==0)
            {
                $invite++;
                curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eventid.'/invited/'.Sql_Result($qresult,$i,"FID"));
                $return = curl_exec($ch);
                if($return == "true")
                {
                    echo "$invite) ".Sql_Result($qresult,$i,"Name") . " has been invited to $eventid!<br>";
                    Sql_Query("INSERT INTO `sdbmwcca_main`.`Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eventid."', '".Sql_Result($qresult,$i,"FID")."');");
                } else
                {
                    echo "<hr><b>$invite) ".Sql_Result($qresult,$i,"Name") . " was not invited because: " . $return . "</b><hr>";
                    curl_close($ch);
                    Sql_Disconnect();
                    die();
                }
            }
            if($invite > ##some spam limit##)
            {
                $x = 0;
                break;
            }
        }
        curl_close ($ch);
        Sql_Disconnect();
    }
}