PHP code example of dipeshchangawala / php-url-checker
1. Go to this page and download the library: Download dipeshchangawala/php-url-checker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dipeshchangawala / php-url-checker example snippets
use Dipeshc\PhpUrlChecker\UrlChecker;
// Initialize the checker with optional Guzzle client settings
$checker = new UrlChecker([
'timeout' => 15 // Set timeout to 15 seconds (optional)
]);
$url = 'https://example.com';
$isValid = $checker->isValidUrl($url);
if ($isValid) {
echo "$url is a valid URL.";
} else {
echo "$url is not valid.";
}
$url = 'https://example.com';
$canonicalUrl = $checker->getCanonicalUrl($url);
if ($canonicalUrl) {
echo "The canonical URL for $url is: $canonicalUrl";
} else {
echo "No canonical URL found for $url";
}
$url = 'http://example.com';
$redirectLocation = $checker->getRedirectLocation($url);
if ($redirectLocation) {
echo "The URL redirects to: $redirectLocation";
} else {
echo "No redirection detected for $url";
}
$url = 'https://example.com';
$isReachable = $checker->isUrlReachable($url);
if ($isReachable) {
echo "$url is reachable.";
} else {
echo "$url is not reachable.";
}
$url = 'http://example.com';
$redirectChain = $checker->getRedirectChain($url);
if (!empty($redirectChain)) {
echo "Redirect chain for $url:\n";
foreach ($redirectChain as $step) {
echo "URL: {$step['url']}, Status: {$step['status']}\n";
}
} else {
echo "$url does not have a redirect chain.";
}
$url = 'https://example.com';
$isSecure = $checker->isSecureUrl($url);
if ($isSecure) {
echo "$url is a secure URL.";
} else {
echo "$url is not secure.";
}