Daily news php scrip

Here is a basic PHP script to fetch and display daily news from a news API:

Note: This script uses the News API (https://newsapi.org/) to fetch news data. You'll need to sign up for a free API key to use this script.

daily_news.php

<?php

// Set your News API key
$apiKey = 'YOUR_API_KEY_HERE';

// Set the news category (e.g. "general", "sports", "entertainment", etc.)
$category = 'general';

// Set the number of news articles to fetch
$limit = 5;

// Set the API endpoint URL
$url = 'https://newsapi.org/v2/top-headlines?category='. $category. '&apiKey='. $apiKey;

// Use cURL to fetch the API response
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

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

// Loop through the news articles and display them
foreach ($data['articles'] as $article) {
    echo '<h2>'. $article['title']. '</h2>';
    echo '<p>'. $article['description']. '</p>';
    echo '<a rel="nofollow" target="_blank" href="'. $article['url']. '">Read more...</a>';
    echo '<br><br>';
}

?>

How it works:

  1. The script sets the News API key, news category, and number of news articles to fetch.
  2. It uses cURL to fetch the API response from the News API endpoint.
  3. The script decodes the JSON response using json_decode().
  4. It loops through the news articles and displays each article's title, description, and URL.

Tips and variations:

Note: This script is just a basic example and may not work as-is. You'll need to modify it to fit your specific needs and requirements. Additionally, be sure to check the News API terms of service to ensure you're using the API correctly.