当频道 ID 来自文本文件时请求频道视频时出现问题 (PHP)

Having issues requesting videos for a channel when the channel ID comes from a text file (PHP)

当我将一些参数设置为从文本文件中提取的变量时,我无法让 youtube api 正常工作。 我有一个不断增长的 channelids 列表,我需要使用它来了解我需要来自哪些频道的视频。 当我直接在代码中分配变量时,它工作正常。 当我循环浏览文本文件并将每个频道 ID 一个一个地插入 url 时,我的 file_get_contents return 什么都没有。

当我执行 print_r 以查看从文件中提取 channelId 时请求 url 是否正确时,url 实际上是正确的。如果我将其复制到浏览器或什至从命令行执行 curl 请求,它实际上可以工作。

然而,出于某种原因,file_get_contents 似乎 return 与 url 毫无关系。

我最初认为问题是我需要将 allow_url_fopen 设置为 On 以便我可以在 php 中使用 url 中的文件中的内容。 不幸的是,这并没有解决问题。

我想将变量传递给

的函数
function createList($API_key,$channelID,$maxResults)   {


 $request = "https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=".$channelID."&key=".$API_key."&maxResults=".$maxResults;
 $videoList = json_decode(file_get_contents($request));
//processes list
}

工作:

createList ($API_key, 'UC1sELGmy5jp5fQUugmuYlXQ', $maxResults, $request);

不工作:

function whitelist($API_key, $maxResults)  {

    $handle = fopen("channels.txt", "r");

    if ($handle) {

        while (($line = fgets($handle)) !== false) {

            $channel = $line;
            createList($API_key, $channel, $maxResults);        
        }

        fclose($handle);
    } else {
        // error opening the file.
    }
}

whitelist($API_key, $maxResults, $request);

也许在您从文本文件中获得的行的开头或结尾有一些 "hidden characters",例如 carriage return or line feed