I am trying to download an image to a remote server using this code.
// Imagen de ejemplo
$imageURL = 'https://keyprint.keystoneindustries.com/wp-content/uploads/2020/01/cast-on-model.png';
$content = @file_get_contents($imageURL);
// mas código....
The error I get is:
Warning: file_get_contents(): SSL: An existing connection was forcibly closed by the remote host.
Try applying these solutions:
But I keep getting the same result.
What can I do to be able to get that image for example?
I think the problem occurs because the server blocks requests that do not come from a browser and it would be enough to create a flow context where the SSL certificate verification is disabled and headers are added to simulate that it is a browser that makes the request:
Result example:
Without disabling certificate verification
Depending on the server where the request will be sent, you can skip the disabling certificate verification part and just simulate the request from a browser:
I recommend you use cURL. It's more powerful and easier to configure, and you'll be able to catch bugs more easily.
Your error may be resolved by adding the
CURLOPT_HTTP_VERSION
appropriate: parameter.I show you a function where we will implement cURL (I wrote it as a function in case you want to reuse it in other contexts).
And to call the function in your context:
In my case, when doing this, the error that you indicate in the question has disappeared, although it is now giving another type of error:
In your context, I think it would be solved by adding other elements to the cURL request that you have for that connection, such as the certificate file or other headers that are required by that site to give you access to its resources. If it doesn't work, the site may be protecting their resources and you should contact them for instructions on how to configure your request.