Skip to Content
PHPCODE
free news api for commercial use php
php code / September 9, 2021

The Live News Data API is a powerful and simple way to obtain live news data from across the world. You may obtain live news and headlines, learn about trends, and keep up with breaking news from around the world. The media stack API is quite handy for displaying news data on a website that lacks content. The mediastack API is the finest option for getting real-time access to legible news data from international news sources.

The mediastack is a basic REST API that distributes historical news data in JSON format from thousands of news websites. Using the mediastack REST API, you may create an automated system to post the most recent and live news articles to your website. The news data is refreshed every minute, ensuring that your application’s blog entries are up to date and in real time. The Mediastack API has a number of features, some of the more useful of which are listed below.

Historical News Data Real-Time News Data News Headlines
There are over 7,500 news sources to choose from.
More than 50 countries
13 different languages

Any programming language can use the mediastack API (PHP, Python, Ruby, jQuery, Nodejs, etc.). In this tutorial, we’ll teach you how to use the mediastack REST API to access live news data and blog posts from around the world using PHP.

To use the mediastack News Data API in PHP, simply follow the steps below.

Get API Access Key

An access key is required to make an API call using mediastack. Create an account on mediastack before you begin. The API key can be found in the dashboard’s Your API Access Key section.

It’s worth noting that mediastack allows you to register a free account and obtain an API key.

Endpoints of APIs
To receive news data, the mediastack provides two API endpoints.
News Data – This is a tool for retrieving current and historical news data.

http://api.mediastack.com/v1/news/index.html
Sources of Information This command is used to get a list of news sources.

http://api.mediastack.com/v1/sources/
http://api.mediastack.com/v1/sources/
http://api.mediastack.com/
HTTPS Encryption: Use HTTPS (SSL) encryption to conduct secure API requests by starting the API URL with a https.

https://api.mediastack.com/v1/news

https://api.mediastack.com/v1/sources

Configuring the API
To authenticate and access the media stack API, you’ll need your Access Key.

To pass needed params in the mediastack API, generate the query string using the http build query() function.
In the access key parameter, enter the API Access Key.

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

PHP with Live News Data
To get live news data, use cURL in PHP to make an HTTP GET request to the mediastack API.

// 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);/* Your code... */

PHP-based news sources
To get a list of news sources, use cURL in PHP to make an HTTP GET request to the mediastack API.

// 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);

Using PHP, retrieve data from an API.
The news and sources data will be delivered in JSON format after a successful API request. To convert a JSON response to an array in PHP, use the json decode() function.

// 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... 
}

Note :

The mediastack API is free to use, however subscription plans for more sophisticated uses are available. We’ve included an example of some of the most useful API configurations in the example code. However, the mediastack API offers a number of customizable options for tailoring the data to your specific requirements. See the mediastack API documentation for a detailed reference.

PHPCODE © 2024