如何在Youtube Api V3中获得下一个20个结果


How To Get Next 20 Results In Youtube Api V3

我正在使用php使用Youtube api v3。我得到了前20个结果,我也得到了一个nextPageToken。我不确定如何在php中使用nextPageToken来获取下一页的结果。这是我当前的代码。

<?php
 if(isset($_POST['submit'])){
 $q = $_POST['query'];
 }
 include_once('header.php');
?>
<?php
if(isset($q)) {
    require_once 'Google/Config.php';
    require_once 'Google/HTTP/REST.php';
    require_once 'Google/Client.php';
    require_once 'Google/Service.php';
    require_once 'Google/Service/Resource.php';
    require_once 'Google/Model.php';
    require_once 'Google/Collection.php';
    require_once 'Google/Service/YouTube.php';
    require_once 'Google/Logger/Abstract.php';
    require_once 'Google/Logger/Null.php';

    /*
    * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
    * Google Developers Console <https://console.developers.google.com/>
    * Please ensure that you have enabled the YouTube Data API for your project.
    */
    $DEVELOPER_KEY = 'Here I have My Key';
    $client = new Google_Client();
    $client->setDeveloperKey($DEVELOPER_KEY);
    // Define an object that will be used to make all API requests.
    $youtube = new Google_Service_YouTube($client);
    try {
        // Call the search.list method to retrieve results matching the specified
        // query term.
        $searchResponse = $youtube->search->listSearch('id,snippet', array(
            'type' => 'video',
            'q' => $q,
            'maxResults' => 20,
            'pageToken' => $searchResponse['nextPageToken']
        ));
        print_r($searchResponse);
        $videoResults = array();
        $cattitle = array();
        # Merge video ids
        foreach ($searchResponse['items'] as $searchResult) {
            array_push($videoResults, $searchResult['id']['videoId']);
        }
        //print_r($searchResponse);

        //echo $searchResult['kind']['nextPageToken'];
        $videoIds = join(',', $videoResults);
        # Code By Shabir.
        $linkarr = explode(',', $videoIds);
        # Code By Shabir.

        # Call the videos.list method to retrieve location details for each video.
        $videosResponse = $youtube->videos->listVideos('snippet, recordingDetails', array(
            'id' => $videoIds,
        ));
        //print_r ($videosResponse);
        //$catname = array();
        // Display the list of matching videos.

        $title = array();
        foreach ($videosResponse['items'] as $videoResult) {
            $videos .= sprintf('<li>%s (%s,%s)</li>',
                array_push($title, $videoResult['snippet']['title']),
                $videocat = $videoResult['snippet']['categoryId'],
                $videoResult['recordingDetails']['location']['latitude'],
                $videoResult['recordingDetails']['location']['longitude']);
            $cat = $youtube->videoCategories->listVideoCategories('snippet', array(
                'id' => $videocat
            ));
        }
        foreach ($cat['items'] as $cati) {
            array_push($cattitle, $cati['snippet']['title']);
            print_r($cattitle[1]);
        }

    } catch (Google_ServiceException $e) {
        $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
            htmlspecialchars($e->getMessage()));
    } catch (Google_Exception $e) {
        $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
            htmlspecialchars($e->getMessage()));
    }
        if(isset($searchResponse['nextPageToken'])) {
            $_SESSION[nextToken]=$searchResponse['nextPageToken'];
        }
        if(isset($searchResponse['prevPageToken'])) {
            $_SESSION[prevToken]=$searchResponse['prevPageToken'];
        }

            <!-- Video Section Start -->
              <?php
              for($i=0; $i<=19; $i++){
              foreach($linkarr as $k=>$value){
                  $value;
                ?>
            <div class="single">
                    <div class="image">
                        <img src="http://img.youtube.com/vi/<?php echo $value;?>/0.jpg" />
                        <div class="hicon gallery">
                            <a href="http://www.youtube.com/watch?v=<?php echo $value;?>?rel=0" rel="prettyPhoto" title="<?php //echo $mainTitle;?>">
                                <img alt="" style="width:48px;height:48px" src="images/zoom.png" />
                            </a>
                        </div>
                        <div class="duration"><span class="ar">&nbsp;&nbsp;&nbsp;&nbsp;</span><?php //echo($time);?></div>
                    </div>

                    <div class="title">
                        <?php print_r($title[$i++]); ?>
                    </div>

                <div class="cat">
                    <p style="display:inline;margin-right:10px">Category: <?php echo $cati['snippet']['title'] ?></p>
                    <p style="display:inline">Views: <?php //echo $vTime ?></p>
                </div>
                <div class="desc"><?php //echo $description; ?></div>
            </div>
          <?php }}}; ?>
            <!-- Video Section End -->
          <div class="clr"></div>
          </div>

          <div class="arrows">
                <div class="rarow">
                    <form method="post" action="page.php">
                            <input type="hidden" name="no" value="20"  />
                            <input type="hidden" name="key" value="<?php echo $q; ?>"  />
                            <input type="submit" value="" name="rpage" class="iarow" />
                    </form>
                </div>
                <div class="aarow">                 
                </div>
                <div class="clr"></div>
          </div>
        </div>
if(isset($searchResponse['nextPageToken'])) {
        $_SESSION['nextToken']=$searchResponse['nextPageToken'];
    }
    if(isset($searchResponse['prevPageToken'])) {
        $_SESSION['prevToken']=$searchResponse['prevPageToken'];
    }

试试这个$_SESSION['nextToken'] " 这个和als