loadHTML($body); libxml_clear_errors(); // Check for meta refresh redirects in HTML $metaTags = $doc->getElementsByTagName('meta'); foreach ($metaTags as $meta) { if (strtolower($meta->getAttribute('http-equiv')) === 'refresh') { $content = $meta->getAttribute('content'); if (preg_match('/\d+;\s*url=(.*)/i', $content, $matches)) { $metaRedirectUrl = trim($matches[1], '"'); // Resolve relative URLs for meta refresh if (!filter_var($metaRedirectUrl, FILTER_VALIDATE_URL)) { $metaRedirectUrl = rtrim($currentUrl, '/') . '/' . ltrim($metaRedirectUrl, '/'); } $currentUrl = $metaRedirectUrl; $redirectCount++; continue 2; // Follow the meta redirect } } } // If no more redirects, return the final URL and content return ['url' => $currentUrl, 'content' => $body, 'error' => $error]; } // If max redirects reached, return an error return ['url' => $currentUrl, 'content' => null, 'error' => 'Too many redirects']; } function extractLinkTags($html) { $doc = new DOMDocument(); @$doc->loadHTML($html); // Suppress warnings due to malformed HTML $linkTags = []; foreach ($doc->getElementsByTagName('link') as $link) { $rel = $link->getAttribute('rel') ?: $link->getAttribute('itemprop'); $href = $link->getAttribute('href') ?: $link->getAttribute('content'); if ($rel && $href) { if (!array_key_exists($rel, $linkTags)) { $linkTags[$rel] = $href; } elseif (!in_array($content, explode('
', $metaData[$name]))) { $linkTags[$rel] .= '
' . $href; } } } return $linkTags; } if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['url'])) { // Get the submitted URL $url = filter_input(INPUT_GET, 'url', FILTER_VALIDATE_URL); if (!$url) { echo "

Invalid URL. Please enter a valid URL.

"; } else { $url_tokens = parse_url($url); if ($url_tokens['scheme'] !== 'https' || filter_var($url_tokens['host'], FILTER_VALIDATE_IP)) { echo "

Invalid URL. Please enter a valid URL.

"; } else { // Fetch the page and follow redirects $result = fetch_page($url); $finalUrl = $result['url']; $htmlContent = $result['content']; $error = $result['error']; if ($htmlContent === null) { echo "

Error: $error

"; } else { // Notify the user of the final URL after following redirects if ($url !== $finalUrl) { $finalUrlEncoded = urlencode($finalUrl); echo "

Note: The request was redirected to {$finalUrl}.

"; } // Load HTML content into DOMDocument $doc = new DOMDocument(); libxml_use_internal_errors(true); // Ignore HTML warnings $doc->loadHTML($htmlContent); libxml_clear_errors(); // Look for tag $metaTags = $doc->getElementsByTagName('meta'); $metaData = []; foreach ($metaTags as $meta) { $name = $meta->getAttribute('name') ?: $meta->getAttribute('property') ?: $meta->getAttribute('itemprop'); $content = $meta->getAttribute('content'); if ($name && $content) { if (!array_key_exists($name, $metaData)) { $metaData[$name] = $content; } elseif (!in_array($content, explode('
', $metaData[$name]))) { //$metaData["item-{$name}"] = $content; $metaData[$name] .= '
' . $content; } } } if ($metaData) { // Define names, emoji icons, and descriptions for common meta tags $metaFriendlyNames = [ // HTML Standard Meta Tags 'title' => ['name' => 'Page Title', 'icon' => '📄', 'description' => 'The title of the webpage, shown on the browser tab.'], 'description' => ['name' => 'Description', 'icon' => '📝', 'description' => 'A short summary of the page content.'], 'keywords' => ['name' => 'Keywords', 'icon' => '🔑', 'description' => 'A list of keywords relevant to the page, used for SEO.'], 'author' => ['name' => 'Author', 'icon' => 'âœī¸', 'description' => 'The author of the webpage.'], 'robots' => ['name' => 'Robots', 'icon' => '🤖', 'description' => 'Instructions for search engines on whether to index the page.'], 'viewport' => ['name' => 'Viewport', 'icon' => '📱', 'description' => 'Defines the visible area of the webpage on different devices.'], 'charset' => ['name' => 'Charset', 'icon' => '🔠', 'description' => 'Character encoding used by the page.'], // Open Graph Meta Tags 'og:title' => ['name' => 'OG Title', 'icon' => '📄', 'description' => 'The title of the page when shared on social platforms.'], 'og:description' => ['name' => 'OG Description', 'icon' => '📝', 'description' => 'A brief description for social sharing.'], 'og:image' => ['name' => 'OG Image', 'icon' => 'đŸ–ŧī¸', 'description' => 'The image displayed when the page is shared.'], 'og:url' => ['name' => 'OG URL', 'icon' => '🔗', 'description' => 'Canonical URL of the page being shared.'], 'og:type' => ['name' => 'OG Type', 'icon' => '🔗', 'description' => 'The type of content (e.g., article, website, video).'], 'og:site_name' => ['name' => 'OG Site Name', 'icon' => '🏠', 'description' => 'The name of the website.'], 'og:locale' => ['name' => 'OG Locale', 'icon' => '🌍', 'description' => 'The locale of the page (e.g., en_US).'], // Twitter Meta Tags 'twitter:title' => ['name' => 'Twitter Title', 'icon' => 'đŸĻ', 'description' => 'The title of the page when shared on Twitter.'], 'twitter:description' => ['name' => 'Twitter Description', 'icon' => 'đŸĻ📝', 'description' => 'A brief description for Twitter sharing.'], 'twitter:image' => ['name' => 'Twitter Image', 'icon' => 'đŸĻđŸ–ŧī¸', 'description' => 'The image displayed when the page is shared on Twitter.'], 'twitter:card' => ['name' => 'Twitter Card', 'icon' => 'đŸĻ🃏', 'description' => 'The type of Twitter card to display (e.g., summary, player).'], // Other common meta tags 'canonical' => ['name' => 'Canonical URL', 'icon' => '🔗', 'description' => 'The preferred URL for the page, used to avoid duplicate content.'], ]; // Display meta tags in table format with names, icons, and raw names echo ""; echo ""; foreach ($metaData as $key => $value) { $friendlyName = $metaFriendlyNames[$key]['name'] ?? ''; // Use friendly name if available $icon = $metaFriendlyNames[$key]['icon'] ?? 'â„šī¸'; // Use icon if available // Update table row to include icon, friendly name, and raw name echo ""; } echo "
Meta TagValue
$icon {$friendlyName} ($key) $value
"; } else { echo "

No meta tags found on the provided URL.

"; } $linkTags = extractLinkTags($htmlContent); if ($linkTags) { // Display the table with elements echo ""; echo ""; foreach ($linkTags as $key => $value) { echo ""; } echo "
Link Reference (rel)Link Href
{$key} {$value}
"; } // Extract important meta information for social media previews $title = $metaData['og:title'] ?? $metaData['twitter:title'] ?? "Untitled"; $description = $metaData['og:description'] ?? $metaData['twitter:description'] ?? "No description available."; $image = $metaData['og:image'] ?? $metaData['twitter:image'] ?? ""; $ogType = $metaData['og:type'] ?? ''; // Only show video if `og:type` is not `video.other` if ($ogType !== 'video.other') { $video = $metaData['og:video'] ?? $metaData['twitter:player'] ?? ""; } else { $video = null; // Ignore video.other type } echo "

Link Preview on Social Platforms

"; // Display cards for different platforms $platforms = [ ['name' => 'WhatsApp', 'color' => '#25D366'], ['name' => 'Telegram', 'color' => '#0088cc'], ['name' => 'Facebook', 'color' => '#3b5998'], ['name' => 'Instagram', 'color' => '#e4405f'], ['name' => 'Pinterest', 'color' => '#bd081c'], ['name' => 'Discord', 'color' => '#7289DA'], ['name' => 'X (formerly Twitter)', 'color' => '#1DA1F2'], ['name' => 'iMessage', 'color' => '#34C759'], ]; foreach ($platforms as $platform) { echo "
"; // Display video if present and og:type is allowed if ($video) { echo ""; } // Display image if available elseif ($image) { echo "Preview Image"; } // Graceful fallback to placeholder else { echo "Placeholder Image"; } // Display title and description echo "
{$platform['name']} $title $description
"; echo "
"; // End of preview card } echo "
"; } } } } ?>