0

在我的网站上,我有一段 php 代码,它下载一些 JSON 并用它所说的内容更新网站。

然而,我的网站变得越来越流行,我的 api 密钥已接近上限。然而,数据仅每十分钟更新一次,所以我认为必须有一种方法只允许脚本每十分钟运行一次,但仍会产生所需的文本。

我的PHP代码:

<?php     
    $json_string = file_get_contents("http://api.wunderground.com/api/MYKEY/conditions/q/pws:IENGLAND469.json");
    $parsed_json = json_decode($json_string);
    $time = $parsed_json->{'current_observation'}->{'observation_time'}; //Take obs time string
    $windmph = $parsed_json->{'current_observation'}->{'wind_mph'};       //take wind speed
    $gustmph = $parsed_json->{'current_observation'}->{'wind_gust_mph'};   //take gust
    $dir = $parsed_json->{'current_observation'}->{'wind_dir'};
    $degs = $parsed_json->{'current_observation'}->{'wind_degrees'};
    $windknots = round($windmph/1.15078,1); //rounds and converts
    $gustmphflt = (float) $gustmph;         //float to string
    $gustknots = round($gustmphflt/1.15078,1); //round and convert
    if($windknots >27){
        echo "<div class='alert alert-danger'><strong>Wooooah!!</strong> Be careful super strong winds of ${windknots}</div>";
    }
    if ($windknots >16 AND $gustknots > 1.7*$windknots  ){
        echo "<div class='alert alert-danger'><strong>Wooooah!!</strong> Be careful it's gust out there!!</div>";
    }
    if($degs>80 AND $degs<200){
        echo "<div class='alert alert-danger'><strong>Be Careful Offshore Wind!!</strong> The wind is offshore, blowing from ${dir}!</div>";
    }
?>

和:

<?php
    echo "<p> <!--From the ${dir} at-->   ${windknots} Knots gusting to ${gustknots} Knots<br>${time}</p>"; //print
?>
4

0 回答 0