Get Live News Data Worldwide with Mediastack API

Are you looking for live and historical news data feed worldwide? it’s very simple. With Mediastack API, you can easily get the live news data feeds, discover trends and headlines, monitor brands and access breaking news events around the world. The API provides realtime news data from thousands of international news publishers and blogs.

The mediastack is a free and simple REST API built upon scalable cloud infrastructure and delivers news results in lightweight and easy-to-use JSON format in multiple languages. If you want to run news website with realtime worldwide news data then you can easily automate system to publish the latest and live news into your website using mediastack REST API. You simply sign up for the free plan, grab your API access key and start implementing news data into your application.

Here is some of the useful features of Mediastack API:

  • Provide Real-Time News Data
  • Historical News Data
  • News Headlines
  • Blog articles
  • Support 7,500+ News Sources
  • 50+ Countries
  • 13 Languages
  • 500 Free Requests per month

The Mediastack API can be integrated using any programming languages such as PHP, Python, Ruby, jQuery, Nodejs, etc. Here in this tutorial, we will explain how to integrate the Mediastack API using PHP to get live news data to use in your website.

How to access Mediastack API

First you need sign up free on Mediastack to get free API Access Key to make API request. In dashboard, you will get the API Access Key under Your API Access Key.


After getting API Access Key, you can start making API call to this awesome news API. Here we will get tennis news data in US.

http://api.mediastack.com/v1/news
    ? access_key = YOUR_ACCESS_KEY
    & keywords = tennis
    & countries = us

If you are already subscribed to the Standard Plan or higher, you will be able to access historical news data by specifying a historical date using the API’s date parameter in YYYY-MM-DD format.

http://api.mediastack.com/v1/news
    ? access_key = YOUR_ACCESS_KEY
    & date = 2021-06-22

As the Mediastack provides news from thousands of sources, so you can also filter down your API results by news sources. You just need to attach the sources parameter to your API request URL and set it to one or multiple news source values. You can also exclude specific news sources by prepending them with a symbol.

http://api.mediastack.com/v1/news
    ? access_key = YOUR_ACCESS_KEY
    & sources = cnn, espn, -cbs

Steps to integrate Mediastack API using PHP

Configure API Request with Options

We need API Access Key to authenticate to access the Mediastack API. Here we will build query string using http_build_query() function and pass access_key to access news API.


$queryString = http_build_query([ 
  'access_key' => 'YOUR_ACCESS_KEY', 
]);

We can also pass other parameters with API request to get API results as per requirement.

$queryString = http_build_query([
  'access_key' => 'ACCESS_KEY',
  'keywords' => 'Tennis',   
  'sort' => 'popularity',
  'countries' => 'us'
]);

Get Live News Data with PHP

Now we will make HTTP GET request to Mediastack API using cURL in PHP to get live news data. Here is the complete PHP code to make API request and get live news data from Mediastack API.

$queryString = http_build_query([
  'access_key' => 'ACCESS_KEY',
  'keywords' => 'Tennis',   
  'sort' => 'popularity',
  'countries' => 'us'
]);

// Intialize Curl to make request
$curlHandle = curl_init();

// News API URL with query string 
$newsApiURL = sprintf('%s?%s', 'http://api.mediastack.com/v1/news', $queryString); 
 
// Set URL and options 
curl_setopt($curlHandle, CURLOPT_URL, $newsApiURL); 
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); 
 
// Execute API request and get response
$responseData = curl_exec($curlHandle); 
 
// Close cURL 
curl_close($curlHandle);

// encode response data 
$newsData = json_decode($responseData, true);

// print response data
print_r($newsData); 

Conclusion

The mediastack API is awesome to create news website with realtime data using various configuration options to customize the data as per your needs. It’s free to use and also premium plans are available for advanced uses. For details, you can checkout the documentation of mediastack API.