Я хочу создать сайт и вставлять новостные статьи.
Я нашел бесплатный сайт newsAPI и получил apikey, как показано ниже:
https://newsapi.org/v1/articles?source=techcrunch&apiKey = 3329a36068a14512b9acb66f2b8f800a
Я хочу, чтобы на моем сайте были живые новости и отображались некоторые главные новости. но я не знаю, как написать правильный код в Laravel. Я пробовал ниже код и только отображать изображение. Я не знаю, как отобразить автора, плитку, описание, URL и т. Д., Например, живой ответ на сайте newsAPI. Ценю, если кто-то может мне помочь.
<?php
$urlArticles = file_get_contents('https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=3329a36068a14512b9acb66f2b8f800a');
$urlArticlesArray = json_decode($urlArticles, true);
$articles = $urlArticlesArray['articles'];
for($i = 0; $i < count($articles); $i++) {
$sites = $urlArticlesArray['articles'][$i];
echo '<img src="'.$sites['urlToImage'].'">';
}
?>
Пример веб-сайта newsAPI:
{
"status": "ok",
"source": "techcrunch",
"sortBy": "top",
-"articles": [
-{
"author": "Lucas Matney",
"title": "Gadgets",
"description": "Product reviews and demos from our TechCrunch Gadget team",
"url": "https://techcrunch.com/video/gadgets/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2013/10/techcrunch-gadgets-icon_rect.png?w=450&h=200&crop=1",
"publishedAt": "2017-04-07T23:00:45Z"},
-{
"author": "Romain Dillet",
"title": "Uber is now banned in Italy for unfair competition",
"description": "While Uber is fighting Waymo at home, the American company is also having issues abroad. As Reuters reported, an Italian court has ordered Uber to stop all..",
"url": "https://techcrunch.com/2017/04/09/uber-is-now-banned-in-italy-for-unfair-competition/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2014/02/uber.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-04-09T09:53:47Z"},
-{
"author": "Ryan Lawler",
"title": "Gig economy stalwart TaskRabbit is contemplating a sale",
"description": "One of the earliest and most prominent startups of the so-called \"sharing economy\" or \"gig economy,\" is evaluating the possibility of selling itself. As..",
"url": "https://techcrunch.com/2017/04/08/taskrabbit-acquisition-maybe/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2015/01/taskrabbit-fire.png?w=764&h=400&crop=1",
"publishedAt": "2017-04-08T23:44:34Z"},
-{
"author": "Ryan Lawler",
"title": "Netflix’s long-time chief product officer Neil Hunt is leaving the company",
"description": "Streaming video provider Netflix is making a change in its senior management, as the company announced long-time chief product officer Neil Hunt will be..",
"url": "https://techcrunch.com/2017/04/08/netflix-neil-hunt-leaving/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/04/los-gatos_01.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-04-08T21:30:52Z"},
-{
"author": "Natasha Lomas",
"title": "Postepic is an app for elegantly sharing book quotes",
"description": "Postepic wants to liberate all those interesting text snippets you have languishing on your camera roll and turn them into visually appealing quotations ready..",
"url": "https://techcrunch.com/2017/04/08/postepic-is-an-app-for-elegantly-sharing-book-quotes/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/04/p1050418.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-04-08T16:00:31Z"}
]
}
Хорошо, во-первых: я бы использовал что-то вроде Guzzle для вызова внешних источников ….
Жадные Документы
Тогда в вашем коде …
$client = new GuzzleHttp\Client();
$url = 'https://.....';
$response = $client->get(url);
dd($response->getStatusCode()); // will dump the statuscode
dd($response->getBody()); // will dump the body
$body = $response->getBody(); //assign body to var
так далее…
Других решений пока нет …