首页 > 解决方案 > 将 json 输出到 php(前端)

问题描述

这是现在在我的索引文件中显示的输出(在 wesbite 上)所以这就是我现在所拥有的 https://gyazo.com/e3b5efb2c0ac9c7225c5c322ae92b0e9

我想让它显示更多nicley(也许在一个带有标题的表格中,然后是文本)

那是我的问题,我不知道我可以谷歌或搜索什么。获取保存在我的 json 文件中的数据。并在我的索引中显示它nicley。

从我的 .json 文件中获取一些值并将它们显示在我的索引中。

这是我的php代码。

$outfile= 'result.json';
$url='https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=MYAPIKEY';
$json = file_get_contents($url);
if($json) {
    if(file_put_contents($outfile, $json, FILE_APPEND)) {
        echo "Saved JSON fetched from “{$url}” as “{$outfile}”.";
    }
    else {
        echo "Unable to save JSON to “{$outfile}”.";
    }
}
else {
    echo "Unable to fetch JSON from “{$url}”.";
}

$option = $_GET['option'];

这就是我在 .json 文件中的显示方式。

{"status":"ok","totalResults":20,"articles":[{"source":{"id":"cnn","name":"CNN"},"author":"Matt McFarland","title":"Has Elon Musk lost control of his hype machine?","description":"For years, the entrepreneur controlled public opinion of Tesla. That's changing.","url":"https://money.cnn.com/2018/07/26/technology/elon-musk-tesla/index.html",

标签: phpjsonapi

解决方案


您可能有无效的 json 响应。首先验证您是否有有效的 json。可能会复制您的回复并在一些验证器网站(如www.jsonlnt.com )中检查它, 如果 json 很好,那么 PHP 函数 json_decode($you_json_string_php_variable); 会正常工作。


推荐阅读