How to insert cnn news feed to bootstrap using php

To insert a CNN news feed into a Bootstrap-powered website using PHP, you'll need to follow these steps:

Step 1: Get a CNN API Key

CNN provides an API for accessing their news articles. You'll need to sign up for a free API key on their website. Once you have the API key, you can use it to fetch news articles.

Step 2: Create a PHP script to fetch CNN news articles

Create a new PHP file (e.g., cnn_news.php) and add the following code:

<?php
// Set your CNN API key
$apiKey = 'YOUR_API_KEY_HERE';

// Set the API endpoint and parameters
$endpoint = 'https://api.cnn.com/v2/article/search';
$params = array(
    'q' => 'news', // Search for news articles
    'sort' => 'published', // Sort by publication date
    'limit' => 10, // Fetch 10 articles
);

// Set the API request headers
$headers = array(
    'Authorization' => 'Bearer '. $apiKey,
    'Content-Type' => 'application/json',
);

// Send the API request
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);

// Decode the JSON response
$data = json_decode($response, true);

// Loop through the articles and extract the relevant information
$articles = array();
foreach ($data['response']['docs'] as $article) {
    $articles[] = array(
        'title' => $article['headline'],
        'description' => $article['short_description'],
        'url' => $article['url'],
        'image' => $article['image'],
        'published_at' => $article['published'],
    );
}

// Output the articles in a format suitable for Bootstrap
echo '<div class="row">';
foreach ($articles as $article) {
    echo '<div class="col-md-4">';
    echo '<h2>'. $article['title']. '</h2>';
    echo '<p>'. $article['description']. '</p>';
    echo '<a rel="nofollow" target="_blank" href="'. $article['url']. '">Read more</a>';
    echo '<img src="'. $article['image']. '" alt="'. $article['title']. '">';
    echo '</div>';
}
echo '</div>';
?>

This script uses the curl extension to send a GET request to the CNN API, and then decodes the JSON response to extract the relevant information (title, description, URL, image, and publication date) from each article. Finally, it outputs the articles in a format suitable for Bootstrap.

Step 3: Include the PHP script in your Bootstrap-powered website

Create a new HTML file (e.g., index.html) and add the following code:

<!DOCTYPE html>
<html>
<head>
    <title>CNN News Feed</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1>CNN News Feed</h1>
        <?php include 'cnn_news.php';?>
    </div>
</body>
</html>

This code includes the cnn_news.php script and displays the output in a Bootstrap container.

Step 4: Run the script and view the results

Save both files and run the script by opening index.html in a web browser. You should see a list of CNN news articles with titles, descriptions, images, and links to read more.

That's it! You've successfully inserted a CNN news feed into your Bootstrap-powered website using PHP.