如何将 Google API 结果保存到数组中并在其上添加新结果?

How do I save Google API results into an array and add new results on it?

我正在尝试制作一个小型 Web 应用程序作为使用 Google API PHP 客户端的辅助项目。在应用程序被用户授予权限后,它会请求用户的播放列表(mine=true 参数),然后它应该将名称和 ID 保存到一个数组中。我遇到的问题是我 PHP 知识有限的结果。

我正在像这样实例化 Google 和 YouTube 客户端:

$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);

然后我向 YouTube API 发出这样的请求:

$listResponse = $youtube->playlists->listPlaylists('snippet', $queryParams);

$listResponse 变量 returns 的结果是正确的,但它是一个让我觉得很奇怪的对象。这个对象包含几个数组,这让我很难存储这些数据。然后是分页的问题。将此请求的结果保存到数组中(以便稍后将其存储在数据库中)并根据分页要求在这些结果之上添加下一页结果的最佳方法是什么?

这是来自 API 的响应示例,有两个结果 (var_dump($listResponse)):

object(Google_Service_YouTube_PlaylistListResponse)#89 (18) {
  ["collection_key":protected]=>
  string(5) "items"
  ["etag"]=>
  string(27) "zQG5YzR-JAU6eqwerdrAindolIs"
  ["eventId"]=>
  NULL
  ["itemsType":protected]=>
  string(31) "Google_Service_YouTube_Playlist"
  ["itemsDataType":protected]=>
  string(5) "array"
  ["kind"]=>
  string(28) "youtube#playlistListResponse"
  ["nextPageToken"]=>
  string(6) "CAQQAA"
  ["pageInfoType":protected]=>
  string(31) "Google_Service_YouTube_PageInfo"
  ["pageInfoDataType":protected]=>
  string(0) ""
  ["prevPageToken"]=>
  string(6) "CAIQAQ"
  ["tokenPaginationType":protected]=>
  string(38) "Google_Service_YouTube_TokenPagination"
  ["tokenPaginationDataType":protected]=>
  string(0) ""
  ["visitorId"]=>
  NULL
  ["internal_gapi_mappings":protected]=>
  array(0) {
  }
  ["modelData":protected]=>
  array(0) {
  }
  ["processed":protected]=>
  array(0) {
  }
  ["pageInfo"]=>
  object(Google_Service_YouTube_PageInfo)#93 (5) {
    ["resultsPerPage"]=>
    int(2)
    ["totalResults"]=>
    int(47)
    ["internal_gapi_mappings":protected]=>
    array(0) {
    }
    ["modelData":protected]=>
    array(0) {
    }
    ["processed":protected]=>
    array(0) {
    }
  }
  ["items"]=>
  array(2) {
    [0]=>
    object(Google_Service_YouTube_Playlist)#58 (17) {
      ["contentDetailsType":protected]=>
      string(45) "Google_Service_YouTube_PlaylistContentDetails"
      ["contentDetailsDataType":protected]=>
      string(0) ""
      ["etag"]=>
      string(27) "IXu48tsFsPRHOKKrr-ITKuPp39A"
      ["id"]=>
      string(34) "PLYZiZDBjPdaZszrfW7goQLqAsde_XNMSs"
      ["kind"]=>
      string(16) "youtube#playlist"
      ["localizationsType":protected]=>
      string(43) "Google_Service_YouTube_PlaylistLocalization"
      ["localizationsDataType":protected]=>
      string(3) "map"
      ["playerType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistPlayer"
      ["playerDataType":protected]=>
      string(0) ""
      ["snippetType":protected]=>
      string(38) "Google_Service_YouTube_PlaylistSnippet"
      ["snippetDataType":protected]=>
      string(0) ""
      ["statusType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistStatus"
      ["statusDataType":protected]=>
      string(0) ""
      ["internal_gapi_mappings":protected]=>
      array(0) {
      }
      ["modelData":protected]=>
      array(0) {
      }
      ["processed":protected]=>
      array(0) {
      }
      ["snippet"]=>
      object(Google_Service_YouTube_PlaylistSnippet)#65 (17) {
        ["collection_key":protected]=>
        string(4) "tags"
        ["channelId"]=>
        string(24) "UCBDfenjx1r7mZgHWksfpOuQ"
        ["channelTitle"]=>
        string(9) "the Legit"
        ["defaultLanguage"]=>
        NULL
        ["description"]=>
        string(0) ""
        ["localizedType":protected]=>
        string(43) "Google_Service_YouTube_PlaylistLocalization"
        ["localizedDataType":protected]=>
        string(0) ""
        ["publishedAt"]=>
        string(20) "2020-01-15T09:03:20Z"
        ["tags"]=>
        NULL
        ["thumbnailsType":protected]=>
        string(39) "Google_Service_YouTube_ThumbnailDetails"
        ["thumbnailsDataType":protected]=>
        string(0) ""
        ["title"]=>
        string(5) "music"
        ["internal_gapi_mappings":protected]=>
        array(0) {
        }
        ["modelData":protected]=>
        array(0) {
        }
        ["processed":protected]=>
        array(0) {
        }
        ["thumbnails"]=>
        object(Google_Service_YouTube_ThumbnailDetails)#91 (16) {
          ["defaultType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["defaultDataType":protected]=>
          string(0) ""
          ["highType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["highDataType":protected]=>
          string(0) ""
          ["maxresType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["maxresDataType":protected]=>
          string(0) ""
          ["mediumType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["mediumDataType":protected]=>
          string(0) ""
          ["standardType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["standardDataType":protected]=>
          string(0) ""
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
          ["default"]=>
          object(Google_Service_YouTube_Thumbnail)#94 (6) {
            ["height"]=>
            int(90)
            ["url"]=>
            string(46) "https://i.ytimg.com/vi/o5AIp2Yc01M/default.jpg"
            ["width"]=>
            int(120)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["medium"]=>
          object(Google_Service_YouTube_Thumbnail)#95 (6) {
            ["height"]=>
            int(180)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/o5AIp2Yc01M/mqdefault.jpg"
            ["width"]=>
            int(320)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["high"]=>
          object(Google_Service_YouTube_Thumbnail)#96 (6) {
            ["height"]=>
            int(360)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/o5AIp2Yc01M/hqdefault.jpg"
            ["width"]=>
            int(480)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
        }
        ["localized"]=>
        object(Google_Service_YouTube_PlaylistLocalization)#97 (5) {
          ["description"]=>
          string(0) ""
          ["title"]=>
          string(5) "music"
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
        }
      }
    }
    [1]=>
    object(Google_Service_YouTube_Playlist)#98 (17) {
      ["contentDetailsType":protected]=>
      string(45) "Google_Service_YouTube_PlaylistContentDetails"
      ["contentDetailsDataType":protected]=>
      string(0) ""
      ["etag"]=>
      string(27) "u8p3zQc4p0ouUduo7o52VI4-tRE"
      ["id"]=>
      string(34) "PLYZiZDBjPdaYHroslJaM21_60yEdP6FZE"
      ["kind"]=>
      string(16) "youtube#playlist"
      ["localizationsType":protected]=>
      string(43) "Google_Service_YouTube_PlaylistLocalization"
      ["localizationsDataType":protected]=>
      string(3) "map"
      ["playerType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistPlayer"
      ["playerDataType":protected]=>
      string(0) ""
      ["snippetType":protected]=>
      string(38) "Google_Service_YouTube_PlaylistSnippet"
      ["snippetDataType":protected]=>
      string(0) ""
      ["statusType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistStatus"
      ["statusDataType":protected]=>
      string(0) ""
      ["internal_gapi_mappings":protected]=>
      array(0) {
      }
      ["modelData":protected]=>
      array(0) {
      }
      ["processed":protected]=>
      array(0) {
      }
      ["snippet"]=>
      object(Google_Service_YouTube_PlaylistSnippet)#99 (17) {
        ["collection_key":protected]=>
        string(4) "tags"
        ["channelId"]=>
        string(24) "UCBDfenjx1r7mZgHWksfpOuQ"
        ["channelTitle"]=>
        string(9) "the Legit"
        ["defaultLanguage"]=>
        NULL
        ["description"]=>
        string(0) ""
        ["localizedType":protected]=>
        string(43) "Google_Service_YouTube_PlaylistLocalization"
        ["localizedDataType":protected]=>
        string(0) ""
        ["publishedAt"]=>
        string(20) "2019-10-04T08:46:06Z"
        ["tags"]=>
        NULL
        ["thumbnailsType":protected]=>
        string(39) "Google_Service_YouTube_ThumbnailDetails"
        ["thumbnailsDataType":protected]=>
        string(0) ""
        ["title"]=>
        string(10) "electronic"
        ["internal_gapi_mappings":protected]=>
        array(0) {
        }
        ["modelData":protected]=>
        array(0) {
        }
        ["processed":protected]=>
        array(0) {
        }
        ["thumbnails"]=>
        object(Google_Service_YouTube_ThumbnailDetails)#100 (18) {
          ["defaultType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["defaultDataType":protected]=>
          string(0) ""
          ["highType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["highDataType":protected]=>
          string(0) ""
          ["maxresType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["maxresDataType":protected]=>
          string(0) ""
          ["mediumType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["mediumDataType":protected]=>
          string(0) ""
          ["standardType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["standardDataType":protected]=>
          string(0) ""
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
          ["default"]=>
          object(Google_Service_YouTube_Thumbnail)#101 (6) {
            ["height"]=>
            int(90)
            ["url"]=>
            string(46) "https://i.ytimg.com/vi/PTxkKPSZyMg/default.jpg"
            ["width"]=>
            int(120)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["medium"]=>
          object(Google_Service_YouTube_Thumbnail)#102 (6) {
            ["height"]=>
            int(180)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/PTxkKPSZyMg/mqdefault.jpg"
            ["width"]=>
            int(320)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["high"]=>
          object(Google_Service_YouTube_Thumbnail)#103 (6) {
            ["height"]=>
            int(360)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/PTxkKPSZyMg/hqdefault.jpg"
            ["width"]=>
            int(480)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["standard"]=>
          object(Google_Service_YouTube_Thumbnail)#104 (6) {
            ["height"]=>
            int(480)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/PTxkKPSZyMg/sddefault.jpg"
            ["width"]=>
            int(640)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["maxres"]=>
          object(Google_Service_YouTube_Thumbnail)#105 (6) {
            ["height"]=>
            int(720)
            ["url"]=>
            string(52) "https://i.ytimg.com/vi/PTxkKPSZyMg/maxresdefault.jpg"
            ["width"]=>
            int(1280)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
        }
        ["localized"]=>
        object(Google_Service_YouTube_PlaylistLocalization)#106 (5) {
          ["description"]=>
          string(0) ""
          ["title"]=>
          string(10) "electronic"
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
        }
      }
    }
  }
}

因为我们不知道你的对象是什么样子,所以我真的不能帮你把对象隐藏成你自己的对象。

我可以告诉你,返回的对象有一个名为 items 的数组,这是播放列表类型的单行。我将从循环 $listRespose->items

开始

至于分页。这样的东西应该可以工作

$token = $results->getNextPageToken();
while($token != null) {    

   $params = array('pageToken' => $token);

   $listResponse = $youtube->playlists->listPlaylists('snippet', $params);

   $token = $results->getNextPageToken();

}