Получите видео YouTube-канала из указанного диапазона c с помощью API goole или любым другим способом - PullRequest
0 голосов
/ 05 апреля 2020

Я пытался получить YouTube видео из списка воспроизведения, используя Google API. Это удалось. Но я могу получить максимум 50 видео. Что мне нужно сделать, это получить более 50 или получить самые старые видео из диапазона.

Как мы можем ограничить результат в mysql.

Языки (PHP & Java Скрипты )

Мой рабочий код

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for youtube.playlistItems.list
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/guides/code_samples#javascript
   */

  function loadClient() {
    gapi.client.setApiKey("MY_API_KEY");
    return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded before calling this method.
  function execute() {
    return gapi.client.youtube.playlistItems.list({
      "part": "snippet,contentDetails",
      "maxResults": 50,
      "playlistId": "PLAY_LIST_ID"
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client");
</script>

Я прочитал документацию по Google API и не получил четкого ответа. Пожалуйста, не закрывайте вопрос. мне нужен четкий ответ.

1 Ответ

0 голосов
/ 06 апреля 2020

<?php

    class GAPI{

        private $appName    = "Get Play List Info";
        private $apiKey     = 'YOUR_API_KEY';
        private $maxResult  = NULL; //0-50
        private $playlistId = NULL;
        private $nextPageToken  = NULL;
        private $prevPageToken  = NULL;

        private $service;
        private $client;

        public function __construct($playlistId,$maxResult=10){

            $this->init(); //Initiate function
            $this->client = new Google_Client(); //Create object from Google Client class

            $this->playlistId = $playlistId; //Set play list id
            $this->maxResult  = $maxResult; //Set max results

            $this->client->setApplicationName($this->appName);
            $this->client->setDeveloperKey($this->apiKey);

            // Define service object for making API requests.
            $this->service = new Google_Service_YouTube($this->client);
        }

        //Initiate
        private function init(){
            if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
              throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
            }
            require_once __DIR__ . '/vendor/autoload.php';
        }

        public function getItems($nextPageToken=NULL,$prevPageToken=NULL){

            $this->nextPageToken = isset($nextPageToken) ? $nextPageToken : NULL;
            $this->prevPageToken = isset($prevPageToken) ? $prevPageToken : NULL;

            $queryParams = [
                'maxResults' => $this->maxResult,
                'playlistId' => $this->playlistId,
                'pageToken'  => $this->nextPageToken
            ];

            $response = $this->service->playlistItems->listPlaylistItems('contentDetails', $queryParams);

            return $response;
        }

    }

?>

...