首页 > 解决方案 > 如何缓存 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>"

?>

请为我的代码提供工作解决方案

标签: phpjsoncaching

解决方案


试试这个,将所有 json 存储在会话中,然后使用另一个页面

session_start(); 
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$_SESSION['data']=$json;

推荐阅读