Last active
April 10, 2018 17:07
-
-
Save nick2687/99b133879846607a77c2 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[!getPageFeed? | |
&fb_app_id=`<your AppId>` | |
&fb_app_secret=`<yourAppSecret>` | |
&page_id=`<fbPageId>` | |
]] |
I have been searching for a way to get a facebook feed into Modx and this is the first thing that has 'semi' worked for me! Thank you! However, as ysanmiguel mentioned - not all the parameters display. Is there an update that will make the picture display?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mate thank you for the code, is easy to use and works... the problem is the RSS/XML only display 3 parameters: time, message and ID. So, all the other parameters in the code are not necessary, same with the template. but technically works, and very well, thank you!