I am currently trying to get and parse events of a user by making use of the Facebook api. In order to do this, I am making use of the function below.
function facebook_get_events($facebookSession) {
// make the API call and get array of event objects
$events = (new FacebookRequest($facebookSession, 'GET', '/me/events')->execute()->getGraphObject();
log("Got events from facebook " . count($events));
foreach ($events as $event) {
echo $event->getProperty('id');
}
return events;
}
Problem: The problem is that its not working. In the log I am noticing that only one element is in the array. By the way, I am assuming that events is an array because, according to Facebook's documentation, the response is "an array of Event objects". Click here to check out the doc link.
With regards to the printing of the id, nothing is being shown.
In order to check whether the request is correct, I also used Facebook's Graph API Explorer, and it is returning the list of events correctly. The result structure is shown below (Information has been removed due to privacy concerns).
{
"data": [
{
"name": "...",
"start_time": "...",
"location": "...",
"rsvp_status": "...",
"id": "..."
},
{
"name": "...",
"start_time": "...",
"timezone": "...",
"rsvp_status": "...",
"id": "..."
}
],
.....
}
I have also checked whether my permissions are correct, and I can confirm that I am including the user_events permission. I think my issue is more related on how I am parsing the response.
As requested by a comment, I have also done a var_dump on events. The following is a snapshot of what I received.
object(Facebook\GraphObject)#5 (1) { ["backingData":protected]=> array(2) { ["data"]=> array(25) { [0]=> object(stdClass)#6 (5) { ["id"]=> string(15) “…” ["name"]=> string(11) “..”. [“location"]=> string(14) “…” ["start_time"]=> string(24) “…”
Progress 1:
I also tried making use of this code, calling the 'data' property, but still did not manage.
function facebook_get_events($facebookSession) {
// make the API call and get array of event objects
$events = (new FacebookRequest($facebookSession, 'GET', '/me/events')->execute()->getGraphObject();
log("Got events from facebook " . count($events->getProperty('data')));
foreach ($events->getProperty('data') as $event) {
echo $event->getProperty('id');
}
return events;
}
The above code prints out nothing.
This is the easiest way to go:
$eventResponse = (new FacebookRequest($session, 'GET', '/me/events'))->execute()->getResponse();
$events = $eventResponse->data;
foreach ($events as $event) {
echo $event->name;
//echo $event->id;
//echo $event->name;
//echo $event->start_time;
//echo $event->timezone;
//echo $event->location;
}
And this is the nice way:
Class declarations:
class GraphEventlist extends GraphObject implements Iterator
{
/**
* @var GraphEvent[]
*/
protected $backingData = array();
public function __construct($raw)
{
foreach ($raw['data'] as $eventArray) {
if ($eventArray instanceof \stdClass) {
$eventArray = get_object_vars($eventArray);
}
$this->backingData[$eventArray['id']] = $eventArray;
}
}
public function current()
{
return $this->getProperty(key($this->backingData), 'GraphEvent');
}
public function key()
{
return key($this->backingData);
}
public function next()
{
next($this->backingData);
}
public function rewind()
{
reset($this->backingData);
}
public function valid()
{
return null !== key($this->backingData);
}
}
class GraphEvent extends GraphObject
{
}
Get events
$events = (new FacebookRequest($session, 'GET', '/me/events'))->execute()->getGraphObject('GraphEventlist');
Iterate:
foreach ($events as $event) {
echo $event->getProperty('name');
}
Get single event by ID:
var_dump($events->getProperty('6499**********4', 'GraphEvent'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With