Live News Data API provides a powerful and easy-to-use way to get worldwide live news data. You can get the live news & headlines, discover trends, and access breaking news from around the world. The mediastack API is very useful to provide news data on the website without having content. If you want to access readable news data from international news publishers in real-time, the mediastack API is the best option to do this job.
The mediastack is a simple REST API that delivers historical news data from thousands of news blogs in easy-to-use JSON format. You can implement an automated system to publish the latest and live news articles into your website using mediastack REST API. The news data is updated every minute, so the blog articles of your application will be the latest and real-time. There are various features available in Mediastack API, some of the most useful features are given below.
The mediastack API can be used with any programming languages (PHP, Python, Ruby, jQuery, Nodejs, etc.). In this tutorial, we will show you how to get live news data and blog articles worldwide with mediastack REST API using PHP.
Follow the below simple steps to integrate mediastack News Data API in PHP.
To make an API request with mediastack, an access key is required.
Note that: You can create a free account and get an API key on mediastack.
The mediastack provides 2 API endpoints to get news data.
News Data – Used to retrieve live and historical news data.
http://api.mediastack.com/v1/news
News Sources – Used to retrieve a list of news sources.
http://api.mediastack.com/v1/sources
HTTPS Encryption:
To make secure API requests use HTTPS (SSL) encryption by calling the API URL begins with an https.
https://api.mediastack.com/v1/news https://api.mediastack.com/v1/sources
The Access Key is required to authenticate and access the media stack API.
http_build_query()
function to pass required params in the mediastack API.access_key
parameter.$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
]);
To fetch the live news data, call mediastack API via HTTP GET request using cURL in PHP.
// Set API access key
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
]);
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.mediastack.com/v1/news', $queryString);
// Initialize cURL
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL
curl_close($ch);
To fetch a list of the news sources, call mediastack API via HTTP GET request using cURL in PHP.
// Set API access key
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'search' => 'abc'
]);
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.mediastack.com/v1/sources', $queryString);
// Initialize cURL
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL
curl_close($ch);
After a successful API request, the news and sources data will be returned in JSON format. Use json_decode()
function to convert the JSON response to array in PHP.
// Convert API json response to array
$api_result = json_decode($api_response, true);
// Output of the API data
foreach ($api_result['data'] as $data) {
// Execution code goes here...
}
The mediastack API is free to use, there also premium plans are available for advanced uses. In the example code, we have provided an example of some most useful API configurations. But, the mediastack API provides various configuration options to customize the data as per your needs. For a complete reference, see the documentation of mediastack API.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Finally i found it
thanks to you
This is very helpful article for me.
Thanks! You