gpt4 book ai didi

php - 如何在 Laravel 中使用 NewsAPI

转载 作者:行者123 更新时间:2023-12-05 07:43:51 26 4
gpt4 key购买 nike

我想建立一个网站并插入新闻文章。我找到了一个免费的 newsAPI 网站,我得到了如下的 apikey: https://newsapi.org/v1/articles?source=techcrunch&apiKey=3329a36068a14512b9acb66f2b8f800a

我希望我的网站有实时新闻并显示一些热门新闻。但我不知道如何在 Laravel 中编写正确的代码。我试过下面的代码并且只显示图像。我不知道如何显示作者、tile、描述、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 之类的东西来调用外部源.... Guzzle Docs

然后,在您的代码中...

$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

等...

关于php - 如何在 Laravel 中使用 NewsAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com