Reputation: 707
I am trying to make a simple file_get_contents(https://some.api) in my code, but, while the URL works in the browser, it doesn't in php. I always get the following error:
file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:0A000126:SSL routines::unexpected eof while reading
I already tried some suggestions from similar questions in Stack Overflown like using stream_context_create():
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$geoData = file_get_contents($requestUrl, false, stream_context_create($arrContextOptions));
But it still doesn't work. PHP SLL also seems to be ok:
~ » php -i | grep -i openssl user@localhost
SSL Version => OpenSSL/3.0.3
libSSH Version => libssh/0.9.6/openssl/zlib
libmongoc SSL library => OpenSSL
openssl
OpenSSL support => enabled
OpenSSL Library Version => OpenSSL 3.0.3 3 May 2022
OpenSSL Header Version => OpenSSL 3.0.2 15 Mar 2022
Openssl default config => /etc/pki/tls/openssl.cnf
openssl.cafile => no value => no value
openssl.capath => no value => no value
Native OpenSSL support => enabled
Upvotes: 2
Views: 17080
Reputation: 1
Conclusion, you need upgrade your php version
In my case using php 7.4, temporarily (while I upgrade) I use '@file_get_contents' instead of 'file_get_contents'
Upvotes: -1
Reputation: 36
This is likely due to this issue: https://bugs.php.net/bug.php?id=79589
An IIS or equivalent non-compliant daemon is not following the SSL specification and OpenSSL is raising an error because of this. PHP does not provide any method to work around it and since you cannot pass the necessary SSL_OP_IGNORE_UNEXPECTED_EOF flag to OpenSSL the connection fails with an error.
For reference:
https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_options.html
SSL_OP_IGNORE_UNEXPECTED_EOF
Some TLS implementations do not send the mandatory close_notify alert on shutdown. If the application tries to wait for the
close_notify alert but the peer closes the connection without sending it, an error is generated. When this option is enabled the peer does not need to send the close_notify alert and a closed connection will be treated as if the close_notify alert was received.
Upvotes: 2