regarding Facebook / JSON integration

I’m trying to include Facebook on a page of my website using my CSS styling. I’m VERY new to PHP and I’m having a lot of trouble getting my code to return any results.

I’m including my code below:

Read More
<?php
/*
Template Name: Facebook display
*/
?>
<div class="wrapper">
<aside id="pageSidebar">
</aside>
<section id="pageContent">
<?php
$page_id = 'MY PAGE';
$access_token = 'ACCESS TOKEN GOES HERE';
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . '/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$posts .= '<div><a href="' . $post->link .'">' . $post->story . '</a></div>';
$posts .= '<div><a href="'. $post->link .'">' . $post->message . '</a></div>';
$posts .= '<div>' . $post->description . '</div>';
$posts .= '<br />';
}
?>
</section>
<div class="clear"></div>
</div>

I then included the following on the page that is supposed to display the Facebook posts

[php]
<?php include 'filename.php'; ?>
[/php]

I’ve activated the php shortcode in WordPress and I set allow_url_fopen = On in my php.ini file. However Nothing. I can’t get the posts to display. I have tested the access token and page id. These are fine. Page is public. Running out of ideas and, honestly, out of my depth here.

Related posts

Leave a Reply

1 comment

  1. Ok, so a lot of this was down to my ignorance of how to integrate PHP into HTML. I was able to do the following:

    <?php
    /*
    Template Name: Facebook display
    */
    ?>
    <div>
    <?php
    $page_id = 'PageID';
    $access_token = 'AccessToken';
    //Get the JSON
    $json_object = @file_get_contents('https://graph.facebook.com/PageID/posts?access_token=AccessToken');
    //Interpret data 
    $fbdata = json_decode($json_object);
    foreach ($fbdata->data as $post )
    {
    $posts .= '<div><h4><a href="' . $post->link .'"><img style="float: left;" height ="100" src="' . $post->picture . '" /></a>';
    $posts .= '<a href="'. $post->link .'">' . $post->message . '</a></h4>';
    $posts .= '<p>' . $post->description . '</p>';
    $posts .= '<hr />';
    }
    echo ($posts);
    ?>  
    </div>
    

    to produce my PHP file.

    From there I made sure my settings were correct in PHP.ini – allow_url_fopen = On being the important one.

    Finally I inserted the following code onto the destination page:

    <table id="var">
    <tbody>
    <tr>
    <td>
    <script>
    $("#var").load("php url");
    </script>
    </td>
    </tr>
    </tbody>
    </table>
    

    And this provided me with the styling I was looking for – at a basic level. I still am working on including like numbers and threaded comments but that’s another issue altogether.

    At least, at the core, I solved my initial problem, which was to push a facebook JSON feed to my destination page with the appropriate CSS applied.