PHP YouTube Api V3 MaxResults не работает

1

Я просто внедрил Google Api в свое приложение, используя сервис YouTube, чтобы получить видео на 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
    //=================================================================================

?>
Теги:
youtube
google-api-php-client

1 ответ

0

Вау, я устал. Я понял.

Изменено

$ htmloutput. = '

к

$ htmloutput = '

Это фиксировало проблему, с которой я столкнулся.

Спасибо вам, ребята!

Ещё вопросы

Сообщество Overcoder
Наверх
Меню