如何缓存JSON请求网址

时间:2019-05-14 10:43:53

标签: php json caching

试图毫无希望地缓存请求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>"

?>

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

1 个答案:

答案 0 :(得分:0)

尝试此操作,将所有json存储在会话中,并在使用另一个页面后

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