php - 如何缓存 JSON 请求 URL
问题描述
试图缓存请求 URL 没有希望
下面是我的 JSON 请求。我想在服务器端缓存 JSON 结果 1 小时。
将我的 JSON 数据存储在我的 PHP 服务器上的 .json 文件中
缓存结果或至少将其保存在会话中,以免在每次页面加载时重复。
<?php
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->GetTickerJSONResult)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->GetTickerJSONResult as $idx => $GetTickerJSONResult) {
// Output a row
$avgPerChangeValue = floatval($GetTickerJSONResult->AvgPerChange);
$symbolName = $GetTickerJSONResult->Symbol ;
$hrefLink = "";
switch ($symbolName) {
case "ALK":
$hrefLink = "https://www.mse.mk/mk/issuer/alkaloid-ad-skopje";
break;
case "GRNT":
$hrefLink = "https://www.mse.mk/mk/issuer/granit-ad-skopje" ;
break;
case "KMB":
$hrefLink = "https://www.mse.mk/mk/issuer/komercijalna-banka-ad-skopje" ;
break;
case "MPT":
$hrefLink = "https://www.mse.mk/mk/issuer/makpetrol-ad-skopje" ;
break;
case "MTUR":
$hrefLink = "https://www.mse.mk/mk/issuer/makedonijaturist-ad-skopje" ;
break;
case "SBT":
$hrefLink = "https://www.mse.mk/mk/issuer/stopanska-banka-ad-bitola" ;
break;
case "TEL":
$hrefLink = "https://www.mse.mk/mk/issuer/makedonski-telekom-ad-%E2%80%93-skopje" ;
break;
case "TNB":
$hrefLink = "https://www.mse.mk/mk/issuer/nlb-banka-ad-skopje" ;
break;
case "TTK":
$hrefLink = "https://www.mse.mk/mk/issuer/ttk-banka-ad-skopje" ;
break;
default:
echo "Does not supported symbol";
}
if ($avgPerChangeValue > 0 ) {
echo "<tr>" ;
echo "<td class='bold-green'>" . "<a href='" . $hrefLink . "'>" . $GetTickerJSONResult->Symbol . "</a></td>";
echo "<td class='bold-green'>$GetTickerJSONResult->AvgPrice</td>";
echo "<td class='bold-green'>$GetTickerJSONResult->AvgPerChange</td>";
echo "</tr>";
} else if ($avgPerChangeValue < 0 ){
echo "<td class='bold-red'>" . "<a href='" . $hrefLink . "'>" . $GetTickerJSONResult->Symbol . "</a></td>";
echo "<td class='bold-red'>$GetTickerJSONResult->AvgPrice</td>";
echo "<td class='bold-red'>$GetTickerJSONResult->AvgPerChange</td>";
echo "</tr>";
} else {
echo "<td class='bold-blue'>" . "<a href='" . $hrefLink . "'>" . $GetTickerJSONResult->Symbol . "</a></td>";
echo "<td class='bold-blue'>$GetTickerJSONResult->AvgPrice</td>";
echo "<td class='bold-blue'>$GetTickerJSONResult->AvgPerChange</td>";
echo "</tr>";
}
}
// Close the table
echo "</table>";
}
echo "<style type='text/css'> td.bold-red { color: red; font-weight: bold; } td.bold-green { color: green; font-weight: bold; } td.bold-blue { color: blue; font-weight: bold; } </style>"
?>
请为我的代码提供工作解决方案
解决方案
试试这个,将所有 json 存储在会话中,然后使用另一个页面
session_start();
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$_SESSION['data']=$json;
推荐阅读
- javascript - 使用多个 if 和对象键改进代码,javascript 中的值
- java - 不显示要移动到 JFrame 的图像
- node.js - npm init 不起作用,ENOENT:没有这样的文件或目录
- directory - 如何修复无限递归文件夹?
- kotlin - 如何在kotlin中将函数的返回类型设置为mutableListOf
- python - 加法计算器的工作没有显示
- javascript - 由于 MIME 类型,无法加载模块脚本
- dns - Office 365登录重定向到 GoDaddy SSO
- kubernetes - Kubernetes v1.20 端点资源无法查看控制器管理器或调度器
- ajax - 响应服务器是否有时间限制