PHP YouTube Api V3 MaxResults不工作

时间:2016-01-21 08:14:35

标签: php linux api youtube google-api-php-client

我刚刚使用YouTube服务将Google Api应用到我的应用中,以获取YouTube视频。就获得结果而言,它按预期工作但由于某种原因,MAXRESULTS无效。它将显示结果,但如果我设置为例子15,那么它在页面上显示无穷无尽的结果。

    <?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
    //=================================================================================

?>

1 个答案:

答案 0 :(得分:0)

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

更改

$ htmloutput。=&#39;

$ htmloutput =&#39;

这解决了我遇到的问题。

谢谢你们!