Get Live News and Blog Articles Worldwide with PHP REST API
source link: https://www.codexworld.com/live-news-blog-articles-worldwide-with-php-rest-api/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Get Live News and Blog Articles Worldwide with PHP REST API
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.
- Real-Time News Data
- Historical News Data
- News Headlines
- 7,500+ News Sources
- 50+ Countries
- 13 Languages
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.
Get API Access Key
To make an API request with mediastack, an access key is required.
- Before getting started, create an account on mediastack.
- In the dashboard, you will get the API key under the Your API Access Key.
Note that: You can create a free account and get an API key on 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:
http://api.mediastack.com/v1/news
News Sources – Used to retrieve a list of news sources.
- End URL:
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
API Configuration
The Access Key is required to authenticate and access the media stack API.
- 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.
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
]);
Live News Data with PHP
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);
News Sources with PHP
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);
Retrieve Data from API using PHP
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...
}
Conclusion
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.
Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request
If you have any questions about this script, submit it to our QA community - Ask Question
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK