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';
$result = $checker->checkStatus($url);

print_r($result);

Array
(
    [url] => https://example.com
    [status] => 200
    [location] =>
)

$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';
$headers = $checker->getHttpHeaders($url);

print_r($headers);

Array
(
    [Content-Type] => text/html; charset=UTF-8
    [Server] => Apache
    [Connection] => keep-alive
)

$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.";
}

$url = 'https://example.com';
$title = $checker->getPageTitle($url);

echo $title;

$url = 'https://jsonplaceholder.typicode.com/todos/1';
$type = $checker->getContentType($url);

echo $type;

$url = 'https://example.com';
$isValid = $checker->validateUrlFormat($url);

$url = 'https://example.com';
$time = $checker->checkResponseTime($url);

echo $time; // in second(with floating point)

$url = 'https://example.com';
$links = $checker->detectBrokenLinks($url);

print_r($links);

$urls = [
    'https://example.com',
    'https://example.com/call2',
    'https://example.com/call3',
];

$links = $checker->checkMultipleUrls($url);

print_r($links);