Is there a way with the Facebook Graph API to get a list of all events created by a single profile? Our client creates a bunch of events and we want to pull a list of them all. I said that they would just have to make sure they set themselves to be attending the event, because then I can easily pull the list of events that profileId is attending, but I'm curious if there's another way. Maybe an FQL query? They look to require a query on the primary key though. And what would that FQL query look like if that's the way to do it??
4 Answers
I basically used the FQL
that @jhchen provided. However, it took me about 6 hours to figure out how to use that FQL
. So I figured, I would provide you a working script. It's not perfect so please update it if you find a bug.
- Download Facebooks PHP SDK
- Add
YOUR_APP_ID
- Add
YOUR_APP_SECRET
- Add
PAGE_ID
- Make changes to the
FQL
statement as you see fit. FQL Documentation
Other than that you should be good to go. Also, I set the default timezone to America/Los_Angeles
. This will probably cause problems for some of you, if anyone has a better way to handle the timezones please let me know.
PHP Code:
<?php
require_once("php/facebook.php");
date_default_timezone_set('America/Los_Angeles');
function display_events()
{
$app_id = 'YOUR_APP_ID';
$secret = 'YOUR_APP_SECRET';
$page_id = 'PAGE_ID';
$config = array();
$config['appId'] = $app_id;
$config['secret'] = $secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$fql = 'SELECT
eid,
name,
pic_square,
creator,
start_time,
end_time
FROM event
WHERE eid IN
(SELECT eid
FROM event_member
WHERE uid='.$page_id.'
)
AND end_time >= ' . mktime() . '
ORDER BY start_time ASC
';
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
$html = '';
foreach($ret_obj as $key)
{
$facebook_url = 'https://www.facebook.com/event.php?eid=' . $key['eid'];
$start_time = date('M j, Y \a\t g:i A', $key['start_time']);
$end_time = date('M j, Y \a\t g:i A', $key['end_time']);
$html .= '
<div class="event">
<a href="'.$facebook_url.'">
<img src="'.$key['pic_square'].'" />
</a>
<span>
<a href="'.$facebook_url.'">
<h2>'.$key['name'].'</h2>
</a>
<p class="time">'.$start_time.'</p>
<p class="time">'.$end_time.'</p>
</span>
</div>
';
}
echo $html;
}
-
1I no longer need to do this and don't have time to test this, but I'm going to assume it works if you've spent 6 hours. Thanks! – jwynveen Nov 18 '11 at 20:25
-
Can you not use the timezone field from the event itself? – Trefex May 27 '13 at 21:25
-
If it has that field available, I don't see why not. It has been awhile since I looked at this script. Sorry I can't be of more help. – zechdc May 29 '13 at 00:48
You can use FQL. Just using the creator as your where clause does not work because the creator is not indexable. However a workaround is to include an indexable where clause and throw in the creator in addition. Ex:
SELECT eid, name, pic, creator FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid=12345) AND creator=12345

- 14,355
- 14
- 63
- 91
This is one of those "there's no good way to get there from here" problems. You can't use FQL, because the creator column isn't indexed. The best solution I can think of at the moment would be using the Graph API to query the events connection of the creator, and filtering out any events that they aren't the creator for. This won't be terribly efficient, since you'll be downloading info about all the user's events, even the ones they didn't create. So, I'd experiment with breaking it down into two queries:
graph.facebook.com/[user_id]/events?fields=id,owner&limit=1000
and then once you've filtered out all the events they didn't create:
graph.facebook.com/?ids=[event_ids for the user's events]

- 56,753
- 31
- 116
- 165
-
You can use FQL, with a where eid in (select eid from event_member where uid=<...>) clause, as described by jcmoney. – Airsource Ltd Oct 05 '10 at 09:25
The answer from zechdc is perfect but I just needed to add strtotime for the start_time to work.
$start_time = date('M j, Y \a\t g:i A', strtotime($key['start_time']));
$end_time = date('M j, Y \a\t g:i A', strtotime($key['end_time']));

- 99
- 4