php - OpenSSL 错误消息 : error:14095126 unexpected eof while reading

标签 php laravel youtube

说明
这个错误:

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 上工作。

你可以尝试的事情。

  • 使用 Curl 代替 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);
    
  • 禁用/卸载 Skpye。由于 skpye 使用端口 80/443(https),​​如果您在端口 80 上使用 apache,请尝试切换到端口 8080 或卸载 skpye(如果您不使用它)。这也可能解决您的问题。
  • 如果要使用,请降级到 Xampp-Windows-7.2.3 file_get_contents()功能。这也为我解决了这个问题。
  • 关于php - OpenSSL 错误消息 : error:14095126 unexpected eof while reading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61635366/

    相关文章:

    php - YouTube网址的preg_match正则表达式部分失败

    php - 如何在 PHP 中重复一个数组?

    php - 为什么在 paypal 集成中金额为零?

    php - 如何从Android应用程序的mysql数据库中检索数据

    php - 如何将 select 的输出转换为 Eloquent 列表?

    php - laravel 在某些情况下自动将数据插入数据库

    php - 为我的页面创建网格外观的最有效方法

    php - pecl/mongodb 需要 PHP (version >= 5.3.0, version <= 5.99.99), 安装的版本是 7.0.4-7 ubuntu 2.1 找不到有效的包安装失败

    javascript - 解析 Youtube 视频源 XML

    javascript - 如何在 youtube 上获取直播视频的可下载网址