php - 将 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",
解决方案
您可能有无效的 json 响应。首先验证您是否有有效的 json。可能会复制您的回复并在一些验证器网站(如www.jsonlnt.com )中检查它, 如果 json 很好,那么 PHP 函数 json_decode($you_json_string_php_variable); 会正常工作。
推荐阅读
- apache-spark - TB 数据写入 parquet
- database - 如何在 Flutter 中从 Firebase 数据库访问单独的子节点?
- r - 如何将同一个csv文件中由标签分隔的多个表读入R-studio?
- python - 添加新列,或访问 numpy 数组中的某个列
- javascript - 获取动态选择选项的值
- cocoapods - cocoapods.org 服务器上没有发布的 pod 版本
- python-3.x - 从另一个列表重命名 Python 中的多个文件
- react-native - 焦点不会从 redux-form 中的 TextInput 改变
- python - 直方图的颜色及其标签不一致
- c++ - apache 箭头 - 读取 csv 文件