A small component to check if the internet is accessible. Original idea by Stackoverflow #4860365. This project adheres to Semantic Versioning.
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();
}
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.
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.