We live in an on-demand atmosphere, so it’s really frustrating to be not able to get the correct news quickly. Live News API data is updated every minute, so your application’s blog posts will be up-to-date and in real time.
The Live News Data API is a convenient and faster way to get live news from around the world. You can get live news & stories, track trends from around the world using Mediastack REST API. The Mediastack API can be used with any programming languages (PHP, Python, Ruby, jQuery, Nodejs, etc.).
Simple REST API for Live News & Blog Articles
Mediastack is an extremely simple REST API interface that gathers news and blog articles around the world and delivers them within milliseconds. It’s doing that using JSON, which means it’s performing great with JavaScript compatible devices like Androids. It will always be current and up-to-date, as Mediastack offers publishers with content that is published every single minute.
There are some of the most useful features available in Mediastack API, and Sources Can You get News From are given below.
Simple steps to integrate mediastack API for Live News in PHP.
In this article, we’ll explain you how to get live news and blog entries around the world using the Mediastack REST API using PHP.
Follow the below simple steps:
Step-1: Get API Access Key
To make any Mediastack API request, an access key is required.
- So Before getting started, create an account on mediastack.
- In your dashboard screen, you will get the API key under the Your API Access Key.
Please refer below image
Step-2: Mediastack API End Points
The mediastack provides 2 API endpoints to get news data.
News Data – Used to retrieve live and historical news data.
- End URL:
https://api.mediastack.com/v1/news
News Sources – Used to retrieve a list of news sources.
- End URL:
https://api.mediastack.com/v1/sources
Note:- HTTPS Encryption
To make secure API requests use HTTPS (SSL) encryption by calling the API URL begins with an https.
Step-3: Make API Request
i) Live News Data with PHP
To fetch the live news data, we will call mediastack API via HTTP GET request using cURL in PHP.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$queryString = http_build_query([ 'access_key' => 'ACCESS_KEY', 'sort' => 'popularity', ]); $ch = curl_init(sprintf('%s?%s', 'https://api.mediastack.com/v1/news', $queryString)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($ch); curl_close($ch); $apiResult = json_decode($json, true); print_r($apiResult); |
In the above code,
- Build the query string using
http_build_query()
function to pass required params in the mediastack API. - Specify the API Access Key in the
access_key
parameter.
ii) News Sources with PHP
To fetch a list of the news sources, call mediastack API using cURL in PHP via HTTP GET request .
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Set API access key $queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'search' => 'abc' ]); // API URL with query string $ch = curl_init(sprintf('%s?%s', 'https://api.mediastack.com/v1/sources', $queryString)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($ch); curl_close($ch); $apiResult = json_decode($json, true); print_r($apiResult); |
Step-4: Retrieve & Display Data from API using PHP
After a successful API request, the news and sources data will be returned in JSON format. In the above code you will get json_decode() function to convert the JSON response to array in PHP. We are using print_r($apiResult) to display the array data.
We can use foreach() to display array data results one by one:
2 3 4 5 6 7 |
// Output of the API data foreach ($apiResult as $data) { // Execution code goes here... } |
Conclusion
If you are looking for readable news details from international news publishers in real-time, the Mediastack API is the best option to do this work. The Mediastack API is very useful for delivering news on the website without any content. You can use the Mediastack REST API to build an automated system to publish the latest and live news articles on your website. For a complete reference, see the documentation of mediastack API.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co