<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$apiKey = "57b15857197b4b399ba7738eefd3decc"; // Replace with your own API key from NewsAPI.
$topic = "technology"; // Change this to the topic you want to fetch news about.
$pageSize = 20;

$newsUrl = "https://newsapi.org/v2/top-headlines?apiKey={$apiKey}&language=en&category={$topic}&pageSize={$pageSize}";

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'MyApp/1.0 (https://newsapi.org, eduard@berekmeri.ro)');

curl_setopt($curl, CURLOPT_URL, $newsUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$newsData = curl_exec($curl);

if ($newsData === false) {
    echo "cURL error: " . curl_error($curl);
    curl_close($curl);
    exit;
}

curl_close($curl);

$newsArray = json_decode($newsData, true);

if (isset($newsArray['code']) && $newsArray['code'] === 'error') {
    echo "Error: " . $newsArray['message'];
    exit;
} elseif (!isset($newsArray['articles'])) {
echo "Error: Unable to fetch news articles. API response:<br><pre>" . print_r($newsArray, true) . "</pre>";

    exit;
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>News about <?php echo ucwords($topic); ?></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>

<body>
    <div class="container mt-4">
        <h1 class="text-center mb-4">News about <?php echo ucwords($topic); ?></h1>
        <div class="row">
            <?php
            foreach ($newsArray['articles'] as $news) {
                $title = $news['title'];
                $description = $news['description'];
                $url = $news['url'];
                $urlToImage = $news['urlToImage'];
                $publishedAt = date("F j, Y, g:i a", strtotime($news['publishedAt']));
            ?>
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <img src="<?php echo $urlToImage; ?>" class="card-img-top" alt="<?php echo $title; ?>">
                        <div class="card-body">
                            <h5 class="card-title"><?php echo $title; ?></h5>
                            <p class="card-text"><?php echo $description; ?></p>
                            <a href="<?php echo $url; ?>" class="btn btn-primary" target="_blank">Read more</a>
                        </div>
                        <div class="card-footer">
                            <small class="text-muted">Published: <?php echo $publishedAt; ?></small>
                        </div>
                    </div>
                </div>
            <?php
            }
            ?>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>

</html>

