Create a PHP script that scrapes a given webpage for all links (URLs) and stores them in a MySQL database. Use DOM parsing to extract the links and PDO for interacting with the database. Prevent duplicate entries in the database and provide error handling for failed connections.
PLAINFORMAT
<?php

// Database configuration
$db_host = 'localhost';
$db_name = 'your_database_name';
$db_user = 'your_username';
$db_pass = 'your_password';

// Webpage to scrape
$webpage_url = 'https://example.com';

// Create a new DOM Document
$doc = new DOMDocument();

// Suppress warnings when loading HTML content
libxml_use_internal_errors(true);

// Load the webpage content
if (!$doc->loadHTMLFile($webpage_url)) {
    die('Failed to load webpage');
}

// Clear libxml error buffer
libxml_clear_errors();

// Extract all links from the DOM
$links = $doc->getElementsByTagName('a');

// Connect to the database using PDO
try {
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die('Database connection failed: ' . $e->getMessage());
}

// Prepare SQL statement for inserting links
$stmt = $pdo->prepare('INSERT IGNORE INTO links (url) VALUES (:url)');

// Iterate through the extracted links
foreach ($links as $link) {
    // Get the href attribute value
    $url = $link->getAttribute('href');

    // Insert the URL into the database (ignoring duplicates)
    try {
        $stmt->execute([':url' => $url]);
    } catch (PDOException $e) {
        echo 'Failed to insert URL (' . $url . '): ' . $e->getMessage() . PHP_EOL;
    }
}

// Close the database connection
$pdo = null;

echo 'Finished scraping and storing URLs';

?>