S
S
Sergey2016-08-31 02:11:33
Yii
Sergey, 2016-08-31 02:11:33

How to check file_get_contents in yii2?

in Yii2 I take data via file_get_contents from a remote page.
If the server is not available or the link is invalid, then yii2 gives an error:

PHP Warning 'yii\base\ErrorException' with message 'file_get_contents(): failed to open stream: Connection refused'

How to do a check to avoid this error? Those. how to check the link?
Link with apache authorization, if it's important. Piece of code:
$context = stream_context_create(array(
          'http' => array(
              'header'  => "Authorization: Basic " . base64_encode("$username:$password")
          )
      ));
      
      $data = file_get_contents($url, false, $context);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-08-31
@butteff

$context = stream_context_create(array(
          'http' => array(
              'header'  => "Authorization: Basic " . base64_encode("$username:$password")
          )
      ));
      
      $data = @file_get_contents($url, false, $context);
      if ($data !== false) {
          //ok
      } else {
          //bad
      }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question