PHP JSON请求重载

时间:2013-09-08 21:41:51

标签: php json

在我的website上我有一段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
?>

0 个答案:

没有答案