Laravel 5 使用Laravel 5.3将Youtube API数据导入数据库

Laravel 5 使用Laravel 5.3将Youtube API数据导入数据库,laravel-5,youtube-data-api,laravel-5.3,Laravel 5,Youtube Data Api,Laravel 5.3,我正在尝试用Laravel5.3构建一个基于Youtube的应用程序。使用库,我能够成功地从播放列表中检索数据并显示它。它的格式如下: [ { 0: { kind: "youtube#playlistItem", etag: ""I_8x5t5r66_FSaexwefRREftGc0/BO3zvggHrzgTTh_ZhXr745ww"", id: "UExDQXc3VFJvaVBIX2VuXzMtcT

我正在尝试用Laravel5.3构建一个基于Youtube的应用程序。使用库,我能够成功地从播放列表中检索数据并显示它。它的格式如下:

[
    {
        0: {
            kind: "youtube#playlistItem",
            etag: ""I_8x5t5r66_FSaexwefRREftGc0/BO3zvggHrzgTTh_ZhXr745ww"",
            id: "UExDQXc3VFJvaVBIX2VuXzMtcThnWW9ZZUc4YlVmX2dOUC4wMTcyMDhGQUE4NTIzM0Y5",
            snippet: {
                publishedAt: "2014-10-06T15:50:12.000Z",
                channelId: "UCGT2vvwtBJ-0edGHEj5Tv67Q",
                title: "S'More - La ricetta dei biscotti",
                description: "Sul blog la storia, la ricetta e le foto",
                thumbnails: {
                    default: {
                        url: "https://i.ytimg.com/vi/Hug-iFvDS3s/default.jpg",
                        width: "120",
                        height: "90"
                     },
                     medium: {
                         url: "https://i.ytimg.com/vi/Hug-iFvDS3s/mqdefault.jpg",
                         width: "320",
                         height: "180"
                     },
                     high: {
                         url: "https://i.ytimg.com/vi/Hug-iFvDS3s/hqdefault.jpg",
                         width: "480",
                         height: "360"
                     },
                     standard: {
                         url: "https://i.ytimg.com/vi/Hug-iFvDS3s/sddefault.jpg",
                         width: "640",
                         height: "480"
                     },
                     maxres: {
                         url: "https://i.ytimg.com/vi/Hug-iFvDS3s/maxresdefault.jpg",
                         width: "1280",
                         height: "720"
                     }
                  },
                  channelTitle: "Ricette di Famiglia",
                  playlistId: "PLCAB2ddt6H_en_3-q8gRfTYsbUf_gNP",
                  position: "0",
                  resourceId: {
                      kind: "youtube#video",
                      videoId: "Hug-iFvDS3s"
                  }
               }
            },
...
]

我的下一步是获取包含视频相关数据的数组,方便地将每个项目映射到视频类实例,最后将每个视频项目存储到我的数据库中。有人知道我怎样才能做到这一点吗?

您可以使用以下方法来实现这一点。根据您在问题中提供的API响应,有一个对象数组,这些对象中有对象,因此需要两个循环

// $json is the JSON response you get from the API
$entities = json_decode($json);

foreach ($entities as $entity) {
    $items = get_object_vars($entity);

    // Each $item is a video object from the API response
    foreach ($items as $item) {
        // Create a new Video object and persist it in the DB
        $video = new Video();
        $video->create([
            'kind' => $item->kind,
            'published_at' => $item->publishedAt,
            ...
        ]);
    }
}