#!/usr/bin/php -q $value) { fwrite(STDERR,"-- $key = $value\n"); fflush(STDERR); } # retrieve this web page # get_contents no longer allowed, uses curl above. # $weatherPage=file_get_contents($weatherURL); # get temperature in Fahrenheit if (preg_match("/([0-9.]+)<\/temp_f>/i",$weatherPage,$matches)) {$currentTemp=$matches[1];} # get relative humidity if (preg_match("/([0-9.]+)<\/relative_humidity>/i",$weatherPage,$matches)) {$currentHumidity=$matches[1];} # get visibility in miles if (preg_match("/([0-9.]+)<\/visibility_mi>/i",$weatherPage,$matches)) {$currentVisibility=$matches[1];} # get wind direction if (preg_match("/North<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='northerly'; } elseif (preg_match("/South<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='southerly'; } elseif (preg_match("/East<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='easterly'; } elseif (preg_match("/West<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='westerly'; } elseif (preg_match("/Northwest<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='northwesterly'; } elseif (preg_match("/Northeast<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='northeasterly'; } elseif (preg_match("/Southwest<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='southwesterly'; } elseif (preg_match("/Southeast<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='southeasterly'; } # get wind speed if (preg_match("/([0-9]+)([\.]+)([0-9]+)<\/wind_mph>/i",$weatherPage,$matches)) { $currentWindSpeed = $matches[1]; } # get barometric pressure if (preg_match("/([0-9]+)([\.]+)([0-9]+)<\/pressure_in>/i",$weatherPage,$matches)) { $currentPressure1 = $matches[1]; $currentPressure2 = $matches[3]; } # tell the caller the current conditions if ($currentTemp) { fwrite(STDOUT,"STREAM FILE weather-conditions \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE temperature \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"SAY NUMBER $currentTemp \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE degrees \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE fahrenheit \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); } # tell the caller the current humidity if ($currentHumidity) { fwrite(STDOUT,"STREAM FILE with \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"SAY NUMBER $currentHumidity \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE percent \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); #fwrite(STDOUT,"STREAM FILE relative \"\"\n"); #fflush(STDOUT); #$result = trim(fgets(STDIN,4096)); #checkresult($result); fwrite(STDOUT,"STREAM FILE humidity \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); } if ($currentWindDirection && $currentWindSpeed) { fwrite(STDOUT,"STREAM FILE $currentWindDirection \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE wind \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE at \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"SAY NUMBER $currentWindSpeed \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE miles-per-hour \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); } # tell the caller the current barometric pressure if ($currentTemp) { fwrite(STDOUT,"STREAM FILE barometric-pressure \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"SAY NUMBER $currentPressure1 \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE point \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"SAY DIGITS $currentPressure2 \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); } # tell caller the current visibility if ($currentVisibility) { fwrite(STDOUT,"STREAM FILE visibility \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"SAY NUMBER $currentVisibility \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); fwrite(STDOUT,"STREAM FILE miles \"\"\n"); fflush(STDOUT); $result = trim(fgets(STDIN,4096)); checkresult($result); } function checkresult($res) { trim($res); if (preg_match('/^200/',$res)) { if (! preg_match('/result=(-?\d+)/',$res,$matches)) { fwrite(STDERR,"FAIL ($res)\n"); fflush(STDERR); return 0; } else{ fwrite(STDERR,"PASS (".$matches[1].")\n"); fflush(STDERR); return $matches[1]; } } else { fwrite(STDERR,"FAIL (unexpected result '$res')\n"); fflush(STDERR); return -1; } } ?>