phpfacebookfacebook-graph-apivideo

facebook graph api video pulling


I'm trying to embed a facebook video gallery on a website.

My problem is that it only shows one video, while the facebook graph shows multiple. How can I make all the videos appear?

<?php
$json_link = "https://graph.facebook.com/sophia.deboer/videos?access_token=862683673820828|15Gx44NW43LHI92o__bRPA6lz44&fields=id,name,description,created_time,from,source&limit=10";
$json = file_get_contents($json_link);
$obj = json_decode($json, true);
$feed_item_count = count($obj['data']);
for ($x = 0; $x < 10; $x++) {
    echo "<div class='item_box'>";
    echo "<div style='overflow:hidden;'>";

    // video source
    $source = $obj['data'][$x]['source'];

    echo "<div class='col-lg-6'>";
    echo "<video src='{$source}' controls>";
    echo "Your browser does not support the video tag.";
    echo "</video>";
    echo "</div>"; // end 'row'

    echo "<div class='col-lg-6'>";

    // user's custom message
    $name = isset($obj['data'][$x]['name']) ? htmlspecialchars_decode($obj['data'][$x]['name']) : "Video #" . $obj['data'][$x]['id'];
    $description = htmlspecialchars_decode(str_replace("\n", "<br>", $obj['data'][$x]['description']));

    // 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_id = $obj['data'][$x]['from']['id'];
    $page_name = $obj['data'][$x]['from']['name'];

    echo "<h2 style='margin: 0 0 .5em 0;'>{$name}</h2>";

    echo "<div>";
    echo $description;
    echo "</div>";

    echo "<div style='margin:.5em 0 0 0; color: #999;'>";
    echo "Posted {$ago_value} by <a href='https://facebook.com/{$page_id}' target='_blank'>{$page_name}</a>";
    echo "</div>";

    echo "</div>";

    echo "</div>";
    echo "<hr />";
    echo "</div>"; // end 'item_box'
}
?>

Demo


Solution

  • After removing :

    $ago_value = time_elapsed_string($converted_date_time);
    The code loops all videos.

    I also changed this:

    $feed_item_count = count($obj['data']);
    for ($x = 0; $x < 10; $x++)
    

    To This:

    $feed_item_count = count($obj['data']);
    for ($x = 0; $x < $feed_item_count; $x++)
    

     <?php
                $json_link = "https://graph.facebook.com/sophia.deboer/videos?access_token=862683673820828|15Gx44NW43LHI92o__bRPA6lz44&fields=id,name,description,created_time,from,source&limit=10";
                $json = file_get_contents($json_link);
                $obj = json_decode($json, true);
                $feed_item_count = count($obj['data']);
                for ($x = 0; $x < $feed_item_count; $x++) {
                    echo "<div class='item_box'>";
                    echo "<div style='overflow:hidden;'>";
    
                    // video source
                    $source = $obj['data'][$x]['source'];
    
                    echo "<div class='col-lg-6'>";
                    echo "<video src='{$source}' controls>";
                    echo "Your browser does not support the video tag.";
                    echo "</video>";
                    echo "</div>"; // end 'row'
    
                    echo "<div class='col-lg-6'>";
    
                    // user's custom message
                    $name = isset($obj['data'][$x]['name']) ? htmlspecialchars_decode($obj['data'][$x]['name']) : "Video #" . $obj['data'][$x]['id'];
                    $description = htmlspecialchars_decode(str_replace("\n", "<br>", $obj['data'][$x]['description']));
    
                    // 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_id = $obj['data'][$x]['from']['id'];
                    $page_name = $obj['data'][$x]['from']['name'];
    
                    echo "<h2 style='margin: 0 0 .5em 0;'>{$name}</h2>";
    
                    echo "<div>";
                    echo $description;
                    echo "</div>";
    
                    echo "<div style='margin:.5em 0 0 0; color: #999;'>";
                    echo "Posted {$ago_value} by <a href='https://facebook.com/{$page_id}' target='_blank'>{$page_name}</a>";
                    echo "</div>";
    
                    echo "</div>";
    
                    echo "</div>";
                    echo "<hr />";
                    echo "</div>"; // end 'item_box'
                }
                ?>