Facebook Posts auf der eigenen Website

Will man Posts von einem Facebook-Account auf seiner eigenen Webseite ausgeben, kann man sich die Daten dafür mittels json holen. Dafür benötigt man die Page-ID(Namen des Profils) und einen Access-Token. Zusätzlich übergebe ich noch die von mir benötigten Felder sowie die Anzahl der Posts. Definiert habe ich das ganze wie folgt:

$fb_page_id = "PageName";
$access_token = "Accestoken";
$fields="id,message,picture,full_picture,link,name,description,type,icon,created_time,from,object_id,shares";
$limit = 10;

$json_link = "https://graph.facebook.com/$fb_page_id/posts?access_token=$access_token&fields=$fields&limit=$limit";

$json = file_get_contents($json_link);
$obj = json_decode($json, true);
$feed_item_count = count($obj['data']);

Mit einer for-Schleife durchlaufe ich dann mein json-Objekt $obj. Alles was ich an Daten benötige speicher ich mir in Variablen zwischen und gebe diese dann auch direkt als HTML-Code aus. Das Bild des Profils hole ich mir per direktem Link, kann auch auf dem Server speichern.

$profile_img = "url-to-profile-image";

for ($x = 0; $x < $feed_item_count; $x++) {

    // to get the post id
    $id = $obj['data'][$x]['id'];
    $post_id_arr = explode('_', $id);
    $post_id = $post_id_arr[1];

    // user's custom message
    $message = $obj['data'][$x]['message'];

    // picture from the link
    $picture = $obj['data'][$x]['full_picture'];
    $picture_url_arr = explode('&url=', $picture);
    $picture_url = urldecode($picture_url_arr[1]);

    // link posted
    $link = $obj['data'][$x]['link'];

    // name or title of the link posted
    $name = $obj['data'][$x]['name'];

    $description = $obj['data'][$x]['description'];
    $type = $obj['data'][$x]['type'];

    // when it was posted
    $created_time = $obj['data'][$x]['created_time'];
    $converted_date_time = date('Y-m-d H:i:s', strtotime($created_time));
    $ago_value = time_elapsed_string($converted_date_time);

    // from
    $page_name = $obj['data'][$x]['from']['name'];
    $profil_picture = $obj['data'][$x]['picture'];

    // useful for photo
    $object_id = $obj['data'][$x]['object_id'];

    $shares = $obj['data'][$x]['shares'];
    $has_picture = strpos($picture, 'https://external.xx.fbcdn.net/safe_image.php?d=AQBfzThSxAH8xP0S&url=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fv%2Ft1.0-1%2Fc10.0.200.200%2Fp200x200%2F1185825_526569817416242_1175123423_n.jpg%3Foh%3D077e713d9a417414c045d1a551063aa5%26oe%3D586B033B');


    echo "<div class='col-md-12 fb-container'>";
    echo "<div class='post-content'>";
    if ($picture && $has_picture === false) {
        echo "<div class='col-md-6'>";
        echo "<img src='{$picture}' />";
        echo "</div>";
        echo "<div class='col-md-6'>";
    } else {
        echo "<div class='col-md-12'>";
    }

    echo "<div class='post-profile'>
            <p class='profile-img'><img src='{$profile_img}' /></p>
            <span>{$page_name}</span><br>
            <span class='post-date'>{$ago_value}</span>
            </div><br><br>";
    echo "<a href='{$link}' target='_blank' class='post-link'><div class='post-info-name'>{$name}</div></a><br>";
    if(!empty($message)){ echo "<div>{$message}</div><br>"; }
    echo "<div class='post-info-description'>{$description}</div>";

    echo "</div></div></div>";
}

Mithilfe von Bootstrap und kleinen CSS-Anpassungen wird der Inhalt ähnlich wie auf Facebook ausgegeben.

Beitrag teilen