facebook graph api 视频拉取

问题描述 投票:0回答:1

我正在尝试在网站上嵌入 Facebook 视频库。

我的问题是它只显示一个视频,而 Facebook 图表显示多个视频。如何让所有视频都出现?

<?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'
}
?>

演示

php facebook facebook-graph-api video
1个回答
1
投票

移除后:

$ago_value = time_elapsed_string($converted_date_time);

该代码循环所有视频。

我也改变了这一点:

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

对此:

$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'
            }
            ?>
© www.soinside.com 2019 - 2024. All rights reserved.