问题
Is there still no way to invite all the group members to an group event? I am able to create the group event via the api, but members are not invited to the new event. If i create the event via the facebook website i have the option to select "invite all group members". I can't seem to find any way to reproduce that functionality via the api.
回答1:
Since there was no way to do this directly via the graph api i ended up making my own php w/mysql solution. Basically i make a table of all my group's members, then i check if they have been invited to all my group's events. the good thing about this forced method is that if a new members joins after an event is created they will be invited to all future events.
facebook does not tell you how many members you can invite from the api per time period and not get flagged for spam
code to dump group members:
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();
}
code to check/invite members:
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();
}
}
来源:https://stackoverflow.com/questions/19676654/creating-and-inviting-a-group-to-an-event-via-graph-api