2016-01-21 85 views
1

我剛剛使用YouTube服務將Google Api應用到我的應用中以獲得youtube視頻。它只要得到結果就能按預期工作,但由於某些原因,MAXRESULTS不起作用。它會顯示結果,但如果我設置例如15,那麼它會在頁面上顯示無數的結果。PHP YouTube Api V3 MaxResults不能正常工作

<?php 
    //Load The Google Api Client 
    //Added Google Api Support 01/21/2016 
    set_include_path(get_include_path().PATH_SEPARATOR.'vendor/google/apiclient/src'); 


    //================================================================================= 
    //START GOOGLE API INTEGRATION 
    //================================================================================= 
    $htmlBody = <<<END 
    <form method="GET"> 
    <div> 
    Search For YouTube Video<br> 
    <input type="search" id="q" name="q" placeholder="SEARCH" size="30"> 
    </div> 
    <input type="submit" value="Search"> 
    </form> 
    END; 

    if ($_GET['q']) 
    { 
    require_once 'Google/Client.php'; 
    require_once 'Google/Service/YouTube.php'; 


    $DEVELOPER_KEY = 'REMOVED FOR OBVIOUS REASONS'; 

    $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(
     'q' => $_GET['q'], 
     'maxResults' => 15, 
     'type' => 'video', 
    )); 

    $videos = ''; 

    // Add each result to the appropriate list, and then display the lists of 
    // matching videos. 
    $i = 0; 
    foreach ($searchResponse['items'] as $searchResult) 
    { 
     switch ($searchResult['id']['kind']) 
     { 
     case 'youtube#video': 
      $videotitle = $searchResult['snippet']['title']; 
      $videoid = $searchResult['id']['videoId']; 

      $videoembed = '<iframe width="150" height="150" src="http://www.youtube.com/embed/'.$videoid.'?autoplay=0&hd=1&vq=hd720" frameborder="0" allowfullscreen></iframe>'; 
      $htmloutput .= ' 
      <table width="50%" align="center"> 
      <tr> 
      <th colspan="2">'.$i.'. '.$videotitle.'</th> 
      </tr> 
      <tr> 
      <td width="40%">'.$videoembed.'</td> 
      <td width="60%" align="center"> 
       <form action="index.php" method="post" id="conversionForm"> 
        <input type="hidden" name="youtubeURL" value="'.$videoid.'">  
        <input type="hidden" value="320" name="quality">  
        <input type="submit" name="submit" value="Create MP3 File"> 
       </form> 
      </td> 
      </tr> 
      </table> 
      '; 
      $videos .= '<li>'.$htmloutput.'</li>'; 
      break; 
     } 
     $i++; 
    } 

    $htmlBody .= <<<END 
    <h3>Videos</h3> 
    <ul>$videos</ul> 
END; 
    } catch (Google_Service_Exception $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())); 
    } 
} 

    //================================================================================= 
    //END GOOGLE API INTEGRATION 
    //================================================================================= 

?> 

回答

0

哇,我一定很累。我想到了。

更改

$ htmloutput。= '

$ htmloutput ='

即固定我是有問題的。

謝謝你們!