php - 如何在 Laravel 中使用 NewsAPI

我想建立一个网站并插入新闻文章。 我找到了一个免费的 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

等...

https://stackoverflow.com/questions/43306949/

相关文章:

android - 使用 ADB shell 命令从 Android 设备读取短信

python - 有没有内置MSMOTE

angular - 如何在 Angular 2的小数管道中使用变量

python - 日志文件的正则表达式

python - 保护或许可 Django 应用程序

amazon-web-services - Amazon Cognito 删除账户

python - 索引 1 超出轴 0 的范围,决策树分类的大小为 1 错误

python - 使用PIL打开图片颜色不对

java - 如果 Spring MVC 中包含特定的 thymeleaf 段,如何从数据库加载数据

javascript - 使用 ng-content 创建一个 Angular2 组件的 "clon