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
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.
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);
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.
Downgrade to Xampp-Windows-7.2.3 if you want to use file_get_contents() function.This also solved the issue for me.
I ran into this issue as well and was able to follow up a little deeper into #Huzaifa99's recommendations.
In my case, I was running a Laravel app with PHP 7.4.5 on Windows. There is apparently an issue with OpenSSL v1.1.1e, possibly just with Windows. Installing the latest build of PHP 7.4.6+ fixed my issues. Specifically, the replacing the file libssl-1_1-x64.dll.
I faced the problem here on Windows 10 with php 7.4.9 and apache v2.4.33.
Although I've updated php to the latest php version (v.7.4.11) the issue did still exist.
It turned out that openssl v1.1.1e which came with apache caused the error. After updating to Apache v2.4.46 with openssl v1.1.1h the problem was solved.
I have same problem on Windows and I found this information:
https://www.php.net/manual/en/function.fopen.php#refsect1-function.fopen-notes
Warning
When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a close_notify indicator. PHP will report this as "SSL: Fatal Protocol Error" when you reach the end of the data. To work around this, the value of error_reporting should be lowered to a level that does not include warnings. PHP can detect buggy IIS server software when you open the stream using the https:// wrapper and will suppress the warning. When using fsockopen() to create an ssl:// socket, the developer is responsible for detecting and suppressing this warning.
I tried to temporary set error_reporting(0); and it worked for me, I got the response from remote server.
But I should use other solution for my case. Maybe this info will help somebody.
in my case I solved it by copying the libssl-1_1-x64.dll dll from the php folder into the apache bin folder
A less brutal version of #SpecDrum's solution worked for me on PHP 8.1.2. The OpenSSL error is represented in PHP as a warning. So it's enough to ignore warnings (temporarily):
$error_level = error_reporting();
error_reporting($error_level & ~E_WARNING);
Related
I'm having issues with Windows MAMP Pro 3.3.1 getting PHP's CURL to work.
More specifically issue is that I can send requests to APIs using curl_exec, but it always returns false.
When I tried to do the same thing with XAMPP it returns full response with no issues.
I tried changing PHP version, I checked that php curl extensions is enabled and I can see CURL info in phpinfo.
Does any one know why and how to fix this issue with MAMP?
So I've seen the issue. Apparently (still need to investigate), a CURL request does not get the certificate from the browser, unlike HTTPS. We'll need to manually download the certificate of the site and add it to the PHP ini.
I got the certificate from here:
https://curl.haxx.se/ca/cacert.pem
Add the path to php.ini. You should see ;curl.cainfo under [curl]. Uncomment and add the path:
curl.cainfo = "path_to_cert\cacert.pem"
Restart apache and it'll work (at least mine did).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
I am having trouble getting either a cURL request or file_get_contents() for a https page that returns JSON. Please note that I have looked at multiple StackOverflow topics with similar problems, but none seem to address my specific issue.
BACKGROUND: I am using a MAC running OS X 10.10.3 (Yosemite). I just installed XAMPP 5.6.8 so that I can run a local web server in order to play around with php, etc.
GOAL: I would like to simply display the contents of a JSON object returned by a https GET request. For context, the page is https://api.discogs.com/releases/249504.
ACTIONS: I have read on other posts that for retrieving HTTPS pages I must make the following out-of-the-box changes in my php.ini file, which I have done:
- uncomment/turn on allow_url_fopen=On
- uncomment/turn on allow_url_include=on
- uncomment extension=php_openssl.dll
RESULTS:
Using file_get_contents()...
Code:
<?php
$url = "https://api.discogs.com/releases/249504";
$text = file_get_contents($url);
var_dump($text);
?>
Result:
Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Applications/XAMPP/xamppfiles/htdocs/phpWorkspace/firstPhp/firstPhp.php on line 5
Warning: file_get_contents(): Failed to enable crypto in /Applications/XAMPP/xamppfiles/htdocs/phpWorkspace/firstPhp/firstPhp.php on line 5
Warning: file_get_contents(https://api.discogs.com/releases/249504): failed to open stream: operation failed in /Applications/XAMPP/xamppfiles/htdocs/phpWorkspace/firstPhp/firstPhp.php on line 5
bool(false)
Using cURL...
Code:
https://api.discogs.com/releases/249504";
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
var_dump($result);
?>
Result:
bool(false)
Any recommendations would be extremely helpful. I've played around with different cURL calls based on other SO posts, each have either returned NULL, an error related to certificates/authentication, or like above.
UPDATE!
I added CURLOPT_SSL_VERIFYPEER to my cURL code and set it to off in order to validate that I could at least communicate with the server - which helped me then identify that I needed to provide a user agent. However, when I removed the SSL_VERIFYPEER, I still had certificate issues.
As a last ditched effort I uninstalled XAMPP, and turned on the php/apache from within the Mac OS. Once configured, out-of-the-box both the the cURL and FILE_GET_CONTENTS() calls worked with the user agent using the same code as above [frustrating!].
My gut tells me that I had a bad configuration in XAMPP related to my certificates. Interestingly though I didn't see much online as this being an issue with other XAMPP users.
Try adding this
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
The certificate is not being successfully verified. This may not correct it depending on how severe the issue is.
You should not rely on this as a fix, however it should tell you if your system can even communicate with theirs. Reference the post Frankzers posted.
What can cause an error 60 for cURL? Certificate is identical to the one used in local environment but raises an error on production.
(original problem)
So I'm trying to integrate Illuminate\Socialize into my app, with a Facebook login. It all seems to work out fine locally, and on my production server I obtain the following error:
RequestException in MultiAdapter.php line 234:
[curl] (#60) See http://curl.haxx.se/libcurl/c/libcurl-errors.html
for an explanation of cURL errors [url]
https://graph.facebook.com/oauth/access_token?client_id={client_id}
&client_secret={secret}&code={code}&redirect_uri={redirect-uri}
It seems that error 60 comes from a certificate error.
A few points:
My Google module works in production as expected.
I'm on shared hosting.
The redirect-uri is authorized
Any directions on a solution would be greatly appreciated!
Edit:
I managed to make it work twisting Adapter\Curl\CurlFactory.php, adding after curl_init
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
Although it is only a quick-fix as it looks insecure. On the same location, I tried adding
curl_setopt($handle, CURLOPT_CAINFO, dirname(__FILE__) . '/../../cacert.pem');
(+verifypeer to true), but it has no effect... anybody?
This might be the same problem as in Guzzle and HTTPS - please see my answer at https://stackoverflow.com/a/29588396/413531 and check if explicitly providing a newly downloaded cacert.pem file solves your issue (double check that the path to the file is valid!)
I'm using aaronpk's Google Voice APIs to send and receive SMS messages in Google Voice. I've uncommented the "extension=php_curl.dll" line in the php.ini and have confirmed that cURL is working. I'm now stuck at this point and I keep receiving this error:
Uncaught exception 'Exception' with
message 'Could not parse for GALX
token'
I've checked all the basic things. The username and password on the account are correct. The only thing that I can see is that cURL is not writing cookie files.
I know the script has a Linux path for the cookiejar / cookiefile by default. I've tried changing this to a Windows directory, as well as including the full path. The code snippet I'm currently using is:
$this->_cookieFile = dirname(__FILE__) . "\cookies.txt";
Even with this code modification, the script is not writing to the cookies.txt file.
I've uploaded these scripts to a Linux host and they work just fine, proving to me that this is a Windows issue. Sadly, we don't have a Linux server for the production environment.
I'm looking for any guidance to get this working within Windows. Right now I'm developing on a Windows 7 machine running XAMPP. The production environment will likely be Windows 2008 Server.
Any assistance would be greatly appreciated!
I know this one is rather old...but it's still nice to share an answer, yeah?
Changing the path of the cookie file is good, but the problem here is with cURL trying (and failing) to verify google's SSL certificate. Two solutions can be found here (I found that link in the accepted answer for this other SO question)
For testing purposes I'd think it would be ok to use the quick and dirty solution (blindly accepting all SSL certificates without verifying).
You'd insert the following line into the GoogleVoice class constructor along with the other curl_setopt lines
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
For production code, I'd verify the certificate. Finding and saving the certificate is covered in the first link I provided. Assuming the certificate is in the same directory as GoogleVoice.php, you'd insert the following lines
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($this->_ch, CURLOPT_CAINFO, getcwd().'\BuiltinObjectToken-VerisignClass3PublicPrimaryCertificationAuthority.crt');
I'm no cURL expert, so I can't say if there's another way to verify the SSL certificate (or why it isn't needed on a Linux host.) This should be all that needs to be changed to get aaronpk's Google Voice API working on XAMPP
It seems that Google has just changed the output HTML for https://accounts.google.com/ServiceLogin in a way that breaks aaronpk's API. I observed the change sometime after December 8th, 2015.
The old HTML: <input name="GALX" type="hidden" value="SecureTokenHere">
The new HTML: <input type="hidden" name="GALX" value="SecureTokenHere">
So if you have the Could not parse for GALX token error, simply update your RegEx from:/name="GALX"\s*type="hidden"\s*value="([^"]+)"/ to /type="hidden"\s*name="GALX"\s*value="([^"]+)"/, or for compatibility, check for the new way if the old way doesn't find a match.
This problem seems to have been discussed in the past everywhere on google and here, but I have yet to find a solution.
A very simple fopen gives me a
PHP Warning: fopen(http://www.google.ca): failed to open stream: HTTP request failed!".
The URL I am fetching have no importance because even when I fetch http://www.google.com it doesnt work. The exact same script works on different server. The one failing is Ubuntu 10.04 and PHP 5.3.2. This is not a problem in my script, it's something different in my server or it might be a bug in PHP.
I have tried using a user_agent in php.ini but no success. My allow_url_fopen is set to On.
If you have any ideas, feel free!
It sounds like your configuration isn't allowed to use file functions, which is common these days because of security concerns. If you have the cURL libraries available to you, I would recommend trying those out.
PHP: cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.ca/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
echo $file;
Check that your php.ini config is set to allow fopen to open external URL's:
allow_url_fopen "1"
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
I'm not at all sure about whether this is the problem or not, but I know in the past I've had problems with opening URLs with fopen, often due to php.ini's allow_url_fopen or other unknown security settings
You may want to try cURL in PHP, which often works for me, you'll find an example really easily by googling that.
Check your phpinfo output - is http present under Registered PHP Streams?
Are you getting "HTTP request failed" without further details? The socket timeout could be expired. This default to 60 seconds. See: http://php.net/manual/en/function.socket-set-timeout.php