From 4eb2b29dd3ebfd943bdea30cf3c6e7d7e05819ef Mon Sep 17 00:00:00 2001 From: arjun163 <54184851+arjun163@users.noreply.github.com> Date: Sun, 5 Nov 2023 19:07:24 +0530 Subject: [PATCH] Update task2.php --- task2.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/task2.php b/task2.php index cb86b2e..159b671 100644 --- a/task2.php +++ b/task2.php @@ -1,2 +1,64 @@ -setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + // URL of the Wikipedia page + $url = 'https://www.wikipedia.org/'; + + // Fetch the page content + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $pageContent = curl_exec($curl); + curl_close($curl); + + // Create a DOMDocument object and load the page content + $dom = new DOMDocument(); + @$dom->loadHTML($pageContent); + + // Extract sections, headings, abstracts, pictures, and links + $sections = $dom->getElementsByTagName('section'); + + // Prepare and execute SQL insert query for each section + $stmt = $pdo->prepare("INSERT INTO wiki_sections (date_created, title, url, picture, abstract) VALUES (NOW(), :title, :url, :picture, :abstract)"); + + foreach ($sections as $section) { + $title = $section->getElementsByTagName('h2')->item(0)->nodeValue; + $abstract = $section->getElementsByTagName('p')->item(0)->nodeValue; + + $links = $section->getElementsByTagName('a'); + $url = ''; + foreach ($links as $link) { + $url = $link->getAttribute('href'); + break; + } + + $pictures = $section->getElementsByTagName('img'); + $picture = ''; + foreach ($pictures as $pic) { + $picture = $pic->getAttribute('src'); + break; + } + + // Execute the SQL insert query + $stmt->bindParam(':title', $title); + $stmt->bindParam(':url', $url); + $stmt->bindParam(':picture', $picture); + $stmt->bindParam(':abstract', $abstract); + $stmt->execute(); + } +} catch (PDOException $e) { + echo 'Error: ' . $e->getMessage(); +}