Skip to content

Instantly share code, notes, and snippets.

@degoya
Forked from nick2687/getPageFeed.php
Created November 19, 2016 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save degoya/11e4222a96f8abce5b8ee44695888df6 to your computer and use it in GitHub Desktop.
Save degoya/11e4222a96f8abce5b8ee44695888df6 to your computer and use it in GitHub Desktop.
Simple modx snippet that will pull facebook page feed data and display it using a chunk
<?php
// Facebook App id & secret
$fb_app_id = isset($fb_app_id) ? $fb_app_id : NULL;
$fb_app_secret = isset($fb_app_secret) ? $fb_app_secret : NULL;
$access_token = $fb_app_id . '|' . $fb_app_secret;
// Other options
$page_id = isset($page_id) ? $page_id : NULL ;
$chunk = isset($chunk) ? $chunk : 'getPageFeedTpl' ;
$limit = isset($limit) ? $limit : 5 ;
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . '/posts?date_format=U&access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);
$output = '';
$i=0;
// For each result, build output values
foreach ($fbdata->data as $post) {
// Get placerholder values
// This has been updated to use search values if present
$placeholders = array(
'created_time' => $post->created_time,
'type' => $post->type,
'status_id' => $post->id,
'message' => $post->message,
'id' => $post->from->id,
'like' => count($post->likes->data[0]->id),
'name' => $post->from->name,
'story' => $post->story,
'picture' => $post->picture,
'picture_link' => $post->link,
);
// Parse chunk passing values
$output .= $modx->getChunk($chunk, $placeholders); // Concatenate to output variable
$i++;
if($i==$limit) break;
}
return $output;
<li class="mix" data-date="[[+created_time:date=`%Y-%m-%d-%H-%M-%S`]]" >
<a class="status_name" href="http://www.facebook.com/profile.php?id=[[+id]]" target="_blank">[[+name]]</a>
<a href="[[+picture_link]]" target="_blank"><img src="[[+picture]]" align="right" /></a>
<p class="status">[[+message]]</p>
<p class="status-info" style="clear:both">
<i class="fa fa-facebook-square"></i>
<span> <a target="_blank" href="https://www.facebook.com/[[+id]]/posts/[[+status_id:stripString=`[[+id]]_`]]">[[+created_time:date=`%Y-%m-%d`:ago]]</a></span></p>
</li>
[[!getPageFeed?
&fb_app_id=`<your AppId>`
&fb_app_secret=`<yourAppSecret>`
&page_id=`<fbPageId>`
]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment