file_get_contents(): Peer certificate did not match

Viewed 30191

I used PHP 5.5 but I forced to update it and now I'm using PHP 5.6.19.

Now, when I'm trying to communicate with external API I get warning:

Warning: file_get_contents(): Peer certificate CN=*.domain.com' did not match expected CN=api.domain.com'

It hasn't appeared in previous PHP version.

    $encryptedEncodedData // this is json encoded
//array, then encrypted by mcrypt with rijndael-128 and finally bin2hex.

    $context = stream_context_create(array(
                        'http' => array(
                            'method' => 'POST',
                            'header' => 'Content-Type: application/json',
                            'content' => $encryptedEncodedData,
                        )
                    ));

    $api = 'https://api.domain.com/service';

    $response = file_get_contents($api, FALSE, $context);

I don't know what is reason for this warning.


I decided to disable peer verfy until my admins will fix problem with cert and I changed $context following:

$context = stream_context_create(array(
                    'http' => array(
                        'method' => 'POST',
                        'header' => 'Content-Type: application/json',
                        'content' => $encryptedEncodedData,
                        'verify_peer'      => false,
                        'verify_peer_name' => false,
                        ), 
                    )
                );

But still not working. Did I do this correct? Getting same Warning.

6 Answers

I know this is old, but I just recently in my life had to deal with this issue and found a solution, so I'm posting it through the web to help others:

I had the same problem and spent countless hours looking for an answer. I hope that I can help the community of developers by sharing the solution to the swift_transportexception connection issue. Here it is...

Make the following adjustment to your .env file:

MAIL_DRIVER=sendmail

MAIL_HOST=YOUR_DOMAIN.COM

MAIL_PORT=465

MAIL_USERNAME=YOUR_EMAIL@YOUR_DOMAIN.COM

MAIL_PASSWORD=YOUR_PASSWORD

MAIL_ENCRYPTION=ssl

MAIL_FROM_ADDRESS=YOUR_EMAIL@YOUR_DOMAIN.COM

MAIL_FROM_NAME="${APP_NAME}"

Make sure your DNS records is set to: MX - @ - mail.YOUR_DOMAIN.COM - Priority 0

If you worry about security, use curl instead of file_get_contents.

$url = "https://www.google.com/"
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);

This function is the same as file get contents but not require you turn of security mode

Related