Retrieve the latest bilingual technology news using Laravel’s HTTP client, cache the response, and render the articles in Blade. The NewTqnia public API requires no API key.
<?php
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;
/**
* Add this self-contained example to routes/web.php.
*
* NewTqnia provides a free public API with no API key required.
* Documentation: https://newtqnia.com/en/developers
*/
Route::get('/latest-technology-news', function () {
/**
* Cache the response for five minutes to improve performance and avoid
* making an external HTTP request on every page load.
*/
$digest = Cache::remember(
'newtqnia.latest.en',
now()->addMinutes(5),
function (): array {
$response = Http::acceptJson()
->timeout(10)
->retry(2, 250, throw: false)
->get('https://newtqnia.com/v1/news/latest', [
// Supported locales are "en" and "ar".
'locale' => 'en',
// The API accepts between 1 and 10 articles.
'limit' => 6,
// Optionally filter using a category slug.
// 'category' => 'artificial-intelligence',
]);
/**
* Return an empty collection when the API is temporarily
* unavailable, allowing the page to remain operational.
*/
return $response->successful()
? $response->json()
: ['articles' => []];
}
);
$articles = collect($digest['articles'] ?? []);
/**
* Build a small, self-contained HTML list.
*
* Laravel's e() helper escapes all API values before inserting them into
* the response, protecting the page from unwanted HTML.
*/
$articleList = $articles->map(function (array $article): string {
$title = e($article['title'] ?? '');
$summary = e($article['summary'] ?? '');
$url = e($article['url'] ?? '#');
$category = e($article['category']['name'] ?? 'Technology');
$publishedAt = e($article['published_at'] ?? '');
$readTime = (int) ($article['read_time'] ?? 0);
return <<<HTML
<article style="margin-bottom: 2rem">
<h2>
<a href="{$url}" target="_blank" rel="noopener noreferrer">
{$title}
</a>
</h2>
<p>
<strong>{$category}</strong>
· {$readTime} min read
</p>
<p>{$summary}</p>
<time datetime="{$publishedAt}">
{$publishedAt}
</time>
</article>
HTML;
})->implode('');
/**
* The API requires visible attribution whenever its content is displayed.
*/
$attributionText = e(
$digest['attribution']['text'] ?? 'Powered by NewTqnia'
);
$attributionUrl = e(
$digest['attribution']['url'] ?? 'https://newtqnia.com'
);
/**
* Display a friendly message if no articles are currently available.
*/
if ($articleList === '') {
$articleList = '<p>No technology news is currently available.</p>';
}
return response(<<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Latest Technology News</title>
</head>
<body>
<main style="max-width: 800px; margin: 3rem auto; padding: 1rem">
<h1>Latest Technology News</h1>
{$articleList}
<footer>
<a
href="{$attributionUrl}"
target="_blank"
rel="noopener noreferrer"
>
{$attributionText}
</a>
</footer>
</main>
</body>
</html>
HTML);
});