【问题标题】:Retrieve ALL video datas with Youtube API V3 PHP使用 Youtube API V3 PHP 检索所有视频数据
【发布时间】:2021-02-01 07:59:02
【问题描述】:

我在使用 YouTube 数据 API 时遇到问题。我正在开发一个 PHP 函数,以从 YouTube 频道中检索 JSON 格式的所有视频信息,然后将其放入文件中。

到目前为止,我设法调用了 API 并检索了前 50 个视频信息;我也知道创建文件的方法。

我知道我必须使用nextPageToken 参数循环该函数,直到没有nextPageToken,但我卡住了。我看过很多类似的帖子解决这个问题,但没有一个真正帮助我。

综上所述,我需要帮助:

  • 使用nextPageToken收集50个视频后的信息;
  • 在循环的每一圈收集此信息。

到目前为止我找到并使用的代码:

function youtube_search($API_key, $channelID, $max_results, $next_page_token=''){
    $myQuery = "https://www.googleapis.com/youtube/v3/search?key=".$API_key."&channelId=".$channelID."&part=snippet,id&order=date&maxResults=".$max_results;
    $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $myQuery);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
            
        curl_close($ch);
            
        $data = json_decode($response);


        if(!empty($data->nextPageToken)){


       return youtube_search($API_key, $channelID, $max_results, $searchResponse['nextPageToken']);
     }

}

youtube_search($API_key, $channelID, $max_results, $next_page_token='');

感谢您的帮助(我是一个谦虚的初学者-_-)。

【问题讨论】:

  • 更具体地说,当您尝试此代码时出了什么问题?
  • $searchResponse 未定义,因此 $searchResponse['nextPageToken'] 将不起作用。您将 $next_page_token 作为函数的参数,但您根本不使用该变量。
  • "在循环的每一圈收集这些信息" 什么循环?你的函数似乎也没有返回任何可以使用的东西,你正在得到$data但没有用它做任何事情。

标签: php youtube-api youtube-data-api


【解决方案1】:

(免责声明:这不是对 OP 问题的实际答案,而只是对下面他自己的答案的代码重构。)

这是您的代码重构,以便使用 do...while 循环而不是尾递归:

function youtube_search_paginated(
    $API_key, $channelID, $max_results, $next_page_token, $videos, $file)
{    
    $dataquery = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=".$channelID."&maxResults=".$max_results."&order=date&key=".$API_key;

    if (!empty($next_page_token))
        $dataquery .= "&pageToken=".$next_page_token;
    
    $ch = curl_init();
        
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $dataquery);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
        
    curl_close($ch);
        
    $data = json_decode($response);

    foreach ($data->items as $item){
        array_push($videos,$item);
    }

    $content = json_encode($videos);
    file_put_contents($file, $content);

    return $data->nextPageToken;
}

function youtube_search(
    $API_key, $channelID, $max_results, $videos, $file)
{
    $next_page_token = NULL;
    do {
        $next_page_token = youtube_search_paginated(
            $API_key, $channelID, $max_results, $next_page_token, $videos, $file);
    } while (!empty($next_page_token));
}

youtube_search($API_key, $channelID, $max_results, $table, $file_name);

【讨论】:

  • 不错!我理解 do...while... 指令。这似乎是执行该功能的一种更合乎逻辑的方式。如果有下一页标记,您添加了一个条件来更改数据查询字符串。谢谢 :) 我认为您更正的代码将帮助很多像我一样陷入无限循环的人
【解决方案2】:

我问了一个朋友,他帮我解决了我的问题。这段代码不起作用,因为我没有正确的论点......以及许多其他问题。此代码用于从频道检索视频信息并将其写入 JSON 文件

    <?php 

$API_key    = 'API_key';
$channelID  = 'channelID';
$max_results = 50;
$table = array();
$file_name = 'all-videos.json';

function youtube_search($API_key, $channelID, $max_results, $next_page_token,$videos,$file){
    
    $dataquery = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=".$channelID."&maxResults=".$max_results."&order=date&pageToken=".$next_page_token."&key=".$API_key;
    
    $ch = curl_init();
        
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $dataquery);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
        
    curl_close($ch);
        
    $data = json_decode($response);


    foreach ($data->items as $item){
        array_push($videos,$item);
    }

    $content = json_encode($videos);
    file_put_contents($file, $content);

    if(!empty($data->nextPageToken)){

        youtube_search($API_key, $channelID, $max_results, $data->nextPageToken, $videos, $file);
    } 
}

youtube_search($API_key, $channelID, $max_results, $next_page_token='', $table, $file_name);
?>

【讨论】:

  • 但是我可以问为什么要使用尾递归而不是简单的do ... while 循环吗?
  • 老实说,我不知道...当我有更多经验时,我会回答这个问题;)
  • 好的,我将在下面为您重构该函数。
猜你喜欢
  • 2013-09-19
  • 2016-08-30
  • 1970-01-01
  • 2013-11-07
  • 2015-07-22
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 2013-04-20
相关资源
最近更新 更多