Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSSL Error messages: error:14095126 unexpected eof while reading

Description
This error:

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14095126:SSL routines:ssl3_read_n:unexpected eof while reading

returns when:

$file = file_get_contents($url['url'], false, stream_context_create($arrContextOptions));

Steps to Reproduce

Video to download:


https://www.youtube.com/watch?v=r5NzAksjfDI

Extract the video id:

$video_id = $yt->extractVideoId(implode($url));

Get the downloadable url

$links = $yt->getDownloadLinks($video_id, $selector = 'video');

Get the array key for the specified video quality etc.

$arrayindex = array_search('mp4, video, 1080p, audio', array_column($links, 'format'));

Set $url to array

$url = $links[$arrayindex];

Disable SSL

$arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), );

Get the file from the url

$video = file_get_contents($url['url'], false, stream_context_create($arrContextOptions));

Send the video back to front end

return view('convert', [ 'url' => $url['url'], 'quality' => $url['format'], ]);

Expected behavior:
The video gets downloaded
Actual behavior:

I get this error:

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14095126:SSL routines:ssl3_read_n:unexpected eof while reading

The code stops.

Version
The latest version of:
https://github.com/Athlon1600/youtube-downloader/blob/master/src/YouTubeDownloader.php

Additional Information
This code is in the laravel framework, the code here is in the UrlController. This is a local development server, this error also shows up when the firewall is disabled. All the functions used in my code are in the YouTubeDownloader.php file

like image 733
birbhouse Avatar asked Dec 02 '22 09:12

birbhouse


1 Answers

I had a similar problem.I suspect you are using Xampp-Windows-7.4.5.In that case file_get_contents() function gives the SSL error on laravel.It works on simple(core) php on my side though.

Things You Can Try.

  1. Use Curl instead of file_get_contents().You can create a custom function that would work in the same manner(with SSL enabled) like so.
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;
}

Then you can call the function like this.

$data = file_get_content_curl($url);
$save = file_put_contents($path, $data);
  1. Disable/Uninstall Skpye.As skpye uses port 80/443(https) ,if you are using apache on port 80 try switching to port 8080 or uninstalling skpye if you dont use it.That might also solve your issue.

  2. Downgrade to Xampp-Windows-7.2.3 if you want to use file_get_contents() function.This also solved the issue for me.

like image 167
Huzaifa Avatar answered May 16 '23 03:05

Huzaifa