按日期排列的 YouTube 视频列表无效

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

我正在运行一段代码来列出我频道中的最新 YouTube 视频。 然而,无论是在这段代码上还是在 YouTube 测试平台上,我确实发布了最后 4 个视频,但以下是旧视频,而不是应该下一个的视频。

这是我的代码:

$video_list = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&type=video&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

    $json = '[';
    
    $i = 0;
    foreach ($video_list->items as $item) {

        if($i > 0){ $json .= ','; }
        $json .= '
        {
            "videoId": "'. $item->id->videoId .'",
            "url": "https://www.youtube.com/watch?v='. $item->id->videoId .'",
            "title": "'. $item->snippet->title .'",
            "description": "'. $item->snippet->description .'",
            "thumbnail": "https://i.ytimg.com/vi/'. $item->id->videoId .'/hqdefault.jpg",
            "publishedAt": "'. $item->snippet->publishedAt .'"
        }';
        $i++;

    } 
    $json .= ']';
    //echo fJson($json);
    file_put_contents('youtube.json', fJson($json));

结果

[
  {
    "videoId": "fSvy6ZHM2vY",
    "title": "Terre de sports - le showdown un sport meéconnu destiné aux non voyants",
    "description": "Lointain cousin du tennis de table et du air hockey, le showdown est un sport étonnant qui commence à trouver de nombreux ...",
    "publishedAt": "2024-04-17T06:46:23Z"
  },
  {
    "videoId": "wgG-SaK3KyM",
    "title": "RECETTE GAGNANTE POUR LES BURGERS SOLIDAIRES DU DÉPARTEMENT",
    "description": "Lundi 15 avril, le Président de la Seine-Maritime, Bertrand Bellanger a officialisé la distribution de la recette de l'opération “burger ...",
    "publishedAt": "2024-04-17T06:35:55Z"
  },
  {
    "videoId": "kjzcF7hFTDE",
    "title": "Carnet d'expos - La beauté cachée des graines",
    "description": "Elles sont symboles de vie, de croissance, de diversité et de culture aussi. Des graines de toutes sortes sont passées sous ...",
    "publishedAt": "2024-04-15T13:52:36Z"
  },
  {
    "videoId": "zGvn6VJWu5E",
    "title": "L'abbaye Saint-Georges, une plongée dans des jardins monastiques",
    "description": "Ces jardins vivriers labellisés « jardin remarquable » avec leur potager, leurs plantes médicinales et leurs fruitiers ont été ...",
    "publishedAt": "2024-04-12T13:15:04Z"
  },
  {
    "videoId": "wrJAjhXGdAw",
    "title": "BOUJOU LA SEINE - Les insolites d'Ariane Au Genetey, un manoir de légendes",
    "description": "Au Genetey, un manoir de légendes : C'est un lieu chargé d'histoires, blotti au fond d'une impasse, en lisière de forêt.",
    "publishedAt": "2023-07-15T06:00:18Z"
  },
  {
    "videoId": "KFoMkpi4KqA",
    "title": "BOUJOU LA SEINE - Les insolites d'Ariane - la nature a trouvé son refuge",
    "description": "À Heurteauville, une ancienne tourbière cache un trésor de biodiversité. Le lieu, classé Espace Naturel Sensible est aujourd'hui ...",
    "publishedAt": "2023-07-08T06:00:12Z"
  },
...

最后 2 个视频不是我应该拥有的,我在 2024 年 4 月发布了更多。有什么问题吗? 谢谢你

php json youtube-api
1个回答
0
投票
<?php

$video_list = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&type=video&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

$data = [];

foreach ($video_list->items as $item) {
    $data[] = [
        'videoId' => $item->id->videoId,
        'url' => 'https://www.youtube.com/watch?v=' . urlencode($item->id->videoId),
        'title' => $item->snippet->title,
        'description' => $item->snippet->description,
        'thumbnail' => "https://i.ytimg.com/vi/{$item->id->videoId}/hqdefault.jpg",
        'publishedAt' => $item->snippet->publishedAt,
    ];
}

// Sort videos by publishedAt, oldest toward beginning
usort($data, function ($a, $b) {
    $time_a = strtotime($a['publishedAt']);
    $time_b = strtotime($b['publishedAt']);
    if ($time_a > $time_b) {
        return 1;
    } elseif ($time_b > $time_a) {
        return -1;
    }
    // $time_a == $time_b
    return 0;
});

// Encode json, with indenting
$json = json_encode($data, JSON_PRETTY_PRINT);

//echo fJson($json);
file_put_contents('youtube.json', fJson($json));
© www.soinside.com 2019 - 2024. All rights reserved.