说明
这个错误:
file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14095126:SSL routines:ssl3_read_n:unexpected eof while reading
返回时:
$file = file_get_contents($url['url'], false, stream_context_create($arrContextOptions));
重现步骤
视频下载:
https://www.youtube.com/watch?v=r5NzAksjfDI
提取视频ID:
$video_id = $yt->extractVideoId(implode($url));
获取可下载的网址
$links = $yt->getDownloadLinks($video_id, $selector = 'video');
获取指定视频质量等的数组键。
$arrayindex = array_search('mp4, video, 1080p, audio', array_column($links, 'format'));
将 $url 设置为数组
$url = $links[$arrayindex];
禁用 SSL
$arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), );
从url获取文件
$video = file_get_contents($url['url'], false, stream_context_create($arrContextOptions));
将视频发送回前端
return view('convert', [ 'url' => $url['url'], 'quality' => $url['format'], ]);
预期行为:
视频已下载
实际行为:
我收到此错误:
file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14095126:SSL routines:ssl3_read_n:unexpected eof while reading
代码停止。
版本
最新版本:
https://github.com/Athlon1600/youtube-downloader/blob/master/src/YouTubeDownloader.php
附加信息
这段代码在laravel框架中,这里的代码在UrlController中。这是本地开发服务器,禁用防火墙时也会显示此错误。我的代码中使用的所有函数都在 YouTubeDownloader.php 文件中
最佳答案
我遇到了类似的问题。我怀疑您使用的是 Xampp-Windows-7.4.5。在这种情况下 file_get_contents()
函数在 laravel 上给出了 SSL 错误。不过它在我这边的简单(核心)php 上工作。
你可以尝试的事情。
file_get_contents()
.您可以像这样创建一个以相同方式(启用 SSL)工作的自定义函数。 function file_get_content_curl ($url)
{
// Throw Error if the curl function does'nt exist.
if (!function_exists('curl_init'))
{
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
然后你可以像这样调用函数。
$data = file_get_content_curl($url);
$save = file_put_contents($path, $data);
file_get_contents()
功能。这也为我解决了这个问题。 关于php - OpenSSL 错误消息 : error:14095126 unexpected eof while reading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61635366/