如何在YouTube api中的php中按标题排序?在search.list的文档中,它说order是一个可选参数。如何按标题或日期订购打印的内容?
尝试{

// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->search->listSearch('id,snippet', array(
  'q' => $_GET['q'],
  'maxResults' => $_GET['maxResults'],
));

$videos = '';
$channels = '';
$playlists = '';
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach ($searchResponse['items'] as $searchResult) {
  switch ($searchResult['id']['snippet']) {
    case 'youtube#video':
      $videos .= sprintf('<li>title=%s link = http://youtube.com/watch?v=%s/ channelid = %s</li><br> ',
          $searchResult['snippet']['title'], $searchResult['id']['videoId'], $searchResult['snippet']['channelTitle']);
      break;

最佳答案

您可以使用order参数按标题,日期,等级,相关性,videoCount和viewCount进行排序。查看订单文档:https://developers.google.com/youtube/v3/docs/search/list

此代码按标题排序

// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->search->listSearch('id,snippet', array(
  'q' => $_GET['q'],
  'maxResults' => $_GET['maxResults'],
  'order' => 'title'
));

该代码按日期排序
// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->search->listSearch('id,snippet', array(
  'q' => $_GET['q'],
  'maxResults' => $_GET['maxResults'],
  'order' => 'date'
));

07-24 18:38