K
K
Kerm2019-08-01 16:16:07
PHP
Kerm, 2019-08-01 16:16:07

How to fix file_get_contents(): SSL: Handshake timed out error?

There is this code:

<?php

function simplexml_load_file_from_url($url, $timeout = 20){
  $opts = array('http' => array('timeout' => (int)$timeout));
  $context  = stream_context_create($opts);
  $data = file_get_contents($url, false, $context);

  if (!$data){
    trigger_error('Cannot load data from url: ' . $url, E_USER_NOTICE);
    return false;
  }

  return simplexml_load_string($data);
}

$url = 'https://geocode-maps.yandex.ru/1.x/?geocode=Москва';

$xml = simplexml_load_file_from_url($url);

From time to time I see errors like this in the logs:
PHP Warning: file_get_contents(): SSL: Handshake timed out in getPickupMap.php on line 32
PHP Warning: file_get_contents(): Failed to enable crypto in getPickupMap.php on line 32
PHP Warning: file_get_contents ( https://geocode-maps.yandex.ru/1.x/?geocode=Moscow): failed to open stream: operation failed in getPickupMap.php on line 32
PHP Notice: Cannot load data from url: https:// geocode-maps.yandex.ru/1.x/?geocode=Moscow in getPickupMap.php on line 35
I can't figure out why these errors appear from time to time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton B, 2019-08-01
@bigton

If added to opts

"ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question