Invite friends to event facebook api

2020-05-09 21:50发布

问题:

My script is pretty slow when inviting users to event, because it has to check if user already is invited to the event, or else it will give a facebook ouath error.

Can i make it faster?

foreach ( $_POST['friends'] as $ids ) {
    if ( $i < 199 ) {
        $iZ = $facebook->api("/".$event_id."/invited/".$ids, "GET");
            if ( $iZ['data'][0]['rsvp_status']):
                    $status = $iZ['data'][0]['rsvp_status'];
            else:
                    $status="";
            endif;

    if ( ($status != 'declined' && $status != 'attending' && $status != "unsure" && $status !=     "not_replied") || empty($status)) {
            $id.=$ids . ",";
            $i=$i+1;
    }
 }
}

$id = substr_replace($id ,"",-1);


echo $facebook->api("/".$event_id . "/invited",'POST', array("users" => $id));

回答1:

Like Cbroe says, use FQL, here is the code:

SELECT uid,name FROM user WHERE uid IN 
(SELECT uid1 FROM friend WHERE uid2=me()) AND NOT 
(uid IN (SELECT uid FROM event_member where eid = 306212359507086))

This will give you the list of friends not invited to that event.

Dont forget to change the event id

Try it on Graph Explorer

Cheers



回答2:

Two possible approaches:

  • use batch requests to post the invite for each user id individually (individually, but still all with one request) – if single invite fails, that does not matter, the following invite posts will still be made.

  • “cross-sect” your friend id list with the FQL event_members table first – get the list of all event members that match your friend ids (using WHERE eid='event_id' AND uid IN(…,…)), and then “subtract” the result (people that have already been invited to the event) from your original friend id list. (In PHP you could use array_diff for that.)