Php 如何在Laravel中使用NewsAPI
我想建立一个网站,插入新闻文章。 我发现了一个免费的newsAPI网站,我得到了如下apikey: 我想我的网站有一个实时新闻和显示一些顶级新闻。但我不知道如何用Laravel编写正确的代码。我试过下面的代码,只显示图像。我不知道如何显示作者,瓷砖,描述,网址等,像新闻API网站的实时响应。如果有人能帮助我,我将不胜感激Php 如何在Laravel中使用NewsAPI,php,laravel,laravel-5,feed,Php,Laravel,Laravel 5,Feed,我想建立一个网站,插入新闻文章。 我发现了一个免费的newsAPI网站,我得到了如下apikey: 我想我的网站有一个实时新闻和显示一些顶级新闻。但我不知道如何用Laravel编写正确的代码。我试过下面的代码,只显示图像。我不知道如何显示作者,瓷砖,描述,网址等,像新闻API网站的实时响应。如果有人能帮助我,我将不胜感激 <?php $urlArticles = file_get_contents('https://newsapi.org/v1/articles?sour
<?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'].'">';
}
?>
好的,首先:我会使用像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
等等。如果
$sites['urlToImage']
为您获取图像,$sites['author']
将为您获取作者,依此类推。这是一样的,只是不同的钥匙。。。
$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