Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Adding Facebook post to personal website

  • 23-07-2013 02:19PM
    #1
    Registered Users, Registered Users 2 Posts: 14


    Hi All,

    Not sure if this is in the right location and please feel free to move if it is.

    I am looking to add the last 3 or so posts from a Business Facebook feed (mine) to my website. It is a HTML site with no plugins

    If anybody has tried this they will understand the following. I have AppID, UserID, AppSecret and Access Token done.

    I am using a PHP script and I can see it is nearly working but not quite. The script is live at www.smartphonetraining.ie/test.php

    If anyone could help fix this I would be very greatful. This is a copy of the script being used:

    <!doctype html>
    <html lang="en">
    <head>
    </head>

    <body>

    <?php

    $FBid = '190722794369142';

    //Get the contents of a Facebook page
    $FBpage = file_get_contents('https://graph.facebook.com/190722794369142/feed?limit=100&access_token=CAACldyp0uI0BAFUYix4ijfVJtIUUbSc2xgZBKmKWVgX5ZAN75TpbGdqsb5qAFnbGavlCaj7xZAau9eaZC9dNmmX8Sf8j6IkaOKyrdVoIC2KwwfHZAGAtQqTt9xsetWZAE3GvMvspad0In86KDJWbDRanhU5VFUjokZD');

    //Interpret data with JSON
    $FBdata = json_decode($FBpage);
    //Set the page name or ID

    //Ttime stamp function
    function timeSince($original) {
    // Array of time period
    $chunks = array(
    array(60 * 60 * 24 * 365 , 'year'),
    array(60 * 60 * 24 * 30 , 'month'),
    array(60 * 60 * 24 * 7, 'week'),
    array(60 * 60 * 24 , 'day'),
    array(60 * 60 , 'hour'),
    array(60 , 'minute'),
    );

    // Current time
    $today = time();
    $since = $today - $original;


    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {

    $seconds = $chunks[$i][0];
    $name = $chunks[$i][1];

    // finding the biggest chunk (if the chunk fits, break)
    if (($count = floor($since / $seconds)) != 0) {
    break;
    }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    if ($i + 1 < $j) {
    // now getting the second item
    $seconds2 = $chunks[$i + 1][0];
    $name2 = $chunks[$i + 1][1];

    // add second item if it's greater than 0
    if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
    $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
    }
    }
    return $print;
    }

    echo '<h1>Facebook News Feed</h1>';

    //Loop through data for each news item
    foreach ($FBdata->data as $news )
    {
    //Explode News and Page ID's into 2 values
    $StatusID = explode("_", $news->id);

    echo '<div class="news-item">';

    //Text/title/description/date
    if (!empty($news->story)) { echo '<h3>' . $news->story . '</h3>'; }
    if (!empty($news->message)) { echo '<h3>' . $news->message . '</h3>'; }
    if (!empty($news->description)) { echo '<p>' . $news->description . '</p>'; }
    echo '<p class="date">Posted '. timeSince(strtotime($news->created_time)) . ' ago</p>';

    //Check for rich media
    if ($news->type == 'link') {
    echo '<a href="'.$news->link.'"><img src="'. $news->picture .'" border="0" style="padding-right:10px;" /></a>';

    //Display link name and description
    if (!empty($news->description)) {
    echo '<a href="'.$news->link.'">'. '<b>' . $news->name . '</b></a>';
    }
    }
    else if ($news->type == 'photo') {
    echo '<a class="photo" href="'.$news->link.'"><img src="'. $news->picture .'" border="0" /></a>';
    }
    else if ($news->type == 'swf') {
    echo '<a href="http://www.facebook.com/permalink.php?story_fbid='.$StatusID.'&id='.$StatusID.'&quot; target="_blank"><img src="'. $news->picture .'" border="0" /></a>';
    }
    else if ($news->type == 'video') {
    //Show play button over video thumbnail
    echo '<a class="vidLink" href="' . $news->source . '"><img class="playBtn" src="images/facebook-playBtn.png" /><img class="poster" src="' . $news->picture . '" alt="' . $news->description . '" /></a>';
    }

    //Check for likes/comments/share counts
    echo '<ul class="meta"><li>Likes: ';

    if (empty($news->likes->count)) { echo '0'; }
    else { echo $news->likes->count; }

    echo '</li><li>Comments: ';

    if (empty($news->comments->count)) { echo '0'; }
    else { echo $news->comments->count; }

    echo '</li><li>Shares: ';

    if (empty($news->shares->count)) { echo '0'; }
    else { echo $news->shares->count . '<br />'; }

    echo '</li></ul>';

    if (!empty($news->link)) { echo '<a class="viewpost" href="' . $news->link . '">View this post on Facebook</a>'; }

    echo '</div> <!-- end .news-item -->';
    }

    ?>

    <div class="likebox"><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like-box href="http://www.facebook.com/<?php echo $FBid ?>" show_faces="true" stream="true" header="true"></fb:like-box></div>

    </body>
    </html>


Comments

  • Registered Users, Registered Users 2 Posts: 81,060 ✭✭✭✭biko


    Since it's code related I'm bouncing this to Dev.


Advertisement