1
votes

I'd like to preface this question with "I am an idiot" but I could really use some help.

I have a PHP site hosted by GoDaddy. I have been using file_get_contents() to read a simple XML page for the past several years into the mySQL database for my site. This January I started getting the below warnings.

Warning: file_get_contents() [function.file-get-contents]: SSL operation failed with code 1. OpenSSL Error messages: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

Warning: file_get_contents() [function.file-get-contents]: Failed to enable crypto

Warning: file_get_contents(https://api.competitionsuite.com/2013-02/Competitions/?o=C35091BF-0F25-4C86-A5AA-C7714E38112C) [function.file-get-contents]: failed to open stream

I did not have an SSL Certificate for the site before this, so I purchased the basic one from GoDaddy hoping this would resolve the error but even on the HTTPS site I get the same errors.

Here is a snippet of my code, but if I need to provide more please let me know.

//read XML
$url = 'https://api.competitionsuite.com/2013-02/Competitions/?o=C35091BF-0F25-4C86-A5AA-C7714E38112C';
$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
                            
$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);

I could really use some guidance if anyone has a spare moment. TIA

1
Its not your certificate its compaining about. Seems like the other end upgraded something (allowed cyphers maybe) and your end cannot keep upRiggsFolly
This is a little over my head, but is the gist that there's nothing I can do?Lance England
What version of PHP are you runningRiggsFolly
Thank you so much @RiggsFolly for having me check out the PHP Version. I updated to PHP version: 5.4.19, which is still not the latest, but that solved my problem with this error and didn't break any of my other php code.Lance England

1 Answers

0
votes

The problem seems to be with the function file_get_contents(..) that doesn't work with SSL (https websites) in simple way.

Simple solution:

You can create function like this - you just need to have enabled curl extension in PHP.

function file_get_contents_ssl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
    curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

It works similar to function file_get_contents(..).

Example:

echo file_get_contents_ssl("https://www.example.com/");

Output:

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
...

Then your code should work this way:

//read XML
$url = 'https://api.competitionsuite.com/2013-02/Competitions/?o=C35091BF-0F25-4C86-A5AA-C7714E38112C';
                            
$xml = file_get_contents_ssl($url);
$xml = simplexml_load_string($xml);