【发布时间】:2021-12-05 15:11:10
【问题描述】:
我的代码需要帮助。我正在从 .JSON 文件 (URL) 中获取内容,我正在尝试优化它,因为它加载速度太慢。所以我有以下代码:
<?php
$site = file_get_contents('website.com/content.json');
$obj = json_decode($site, true);
for ($i = 0; $i <= 9; $i++) {
echo '<div class="result-' . $obj[$i]['id'] . '"></div>';
}
?>
所以一切看起来都很好,对吧?但困难的部分来了。在这个简单的循环中,我需要创建另一个 URL 请求,以便我可以从另一个 URL 获取有关当前结果的信息。我们开始:
<?php
$site = file_get_contents('website.com/content.json');
$obj = json_decode($site, true);
for ($i = 0; $i <= 9; $i++) {
echo '<div class="result-' . $obj[$i]['id'] . '">';
$info = json_decode(file_get_contents("website.com/info/" . $obj[$i]['id'] . ".json), true);
echo $info['content'];
echo '</div>';
}
?>
因此,由于需要运行许多查询,这会导致页面加载速度变慢。知道该怎么做吗? (我无法将附加信息保存在单个存储中。它必须在不同的 URL 上)
【问题讨论】:
-
您调用的网站是您的吗?即你能改变 api 的工作方式吗
-
@RiggsFolly 不,我打电话的网站不是我的。
-
小点如果你要将一个完美的对象转换成数组
$obj = json_decode($site, true);为什么调用变量$obj而不是$array:) -
那没有办法优化这个
-
但如果没有,您可能没有办法做到这一点。向 API 提供者投诉。
标签: php json file-get-contents