lfischer / internet-available
A small component to check if the internet is currently available / accessible
1.0.1
2020-01-23 16:15 UTC
Requires
- php: >=7.1
- psr/log: ^1.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.2
- friendsofphp/php-cs-fixer: ^2.16
- vimeo/psalm: ^3.8
This package is auto-updated.
Last update: 2025-04-24 04:00:10 UTC
README
A small component to check if the internet is accessible. Original idea by Stackoverflow #4860365. This project adheres to Semantic Versioning.
Usage
The very basic usage (with default parameters) looks like this:
$available = \lfischer\internet\Internet::available();
But of course there are some options you can provide to handle the responses as you need them:
use \lfischer\internet\Internet; use \lfischer\internet\InternetException; use \lfischer\internet\InternetProblemException; // Check the availability by connecting to Google on port 80. $available = (new Internet('www.google.com', 80))->check(); // try { $internet = new Internet( 'www.google.com', 80, Internet::EXCEPTION_ON_UNAVAILABILITY + Internet::PROBLEM_AS_EXCEPTION ); $available = $internet->check(); } catch (InternetException $e) { // The internet is not available. $internet->getErrorString(); $internet->getErrorNumber(); $e->getMessage(); } catch (InternetProblemException $e) { // There was a problem while checking the availability. $internet->getErrorString(); $internet->getErrorNumber(); $e->getMessage(); }
Options
You can pass some options to change the behaviour in case of problems.
Internet::EXCEPTION_ON_UNAVAILABILITY
Will throw anInternetException
exception if the internet is unavailable (instead of returningfalse
).Internet::PROBLEM_AS_EXCEPTION
Will throw anInternetProblemException
exception if the internet availability could not be checked due to a problem (instead of returningfalse
).Internet::PROBLEM_AS_TRUE
Will returntrue
in case of a problem during the availability check since you'd like to assume something else went wrong.
Static code analysis and code style
The code is being statically analyzed with the help of vimeo/psalm. The PSR2 code style will be checked/applied with the help of friendsofphp/php-cs-fixer.