drexnefex Posted January 4, 2007 Share Posted January 4, 2007 Hello - I am getting the following error: Warning: socket_set_timeout(): supplied argument is not a valid stream resource in /testbuoy4.php on line 179.This script was working fine a few days ago and suddenly broke. I haven't edited the file recently and am completely stumped on why it's broken.Any ideas out there?[code]<?############ phpBuoy ############## Version: 1.0.2# Last modified: May 28, 2003## Author: Steve Lange - [email protected]# Homepage: http://www.surfimplement.com/phpBuoy/#################### LICENSE INFO ################### This file is released under the terms of the # Gnu Public License available for viewing at:# http://www.surfimplement.com/gpl.txt## Feel free to modify this script as you see fit, # but respect the terms of the license.### phpBuoy was created in part with code from # Kalle Kiviaho's Newsbackends script, for # parsing headline files using PHP. You can # check it out at: # http://swamp.chl.chalmers.se/backends/# ######################### HOW TO USE phpBuoy ######################### phpBuoy is meant to be used as either a PHP # or SSI include file, and of course must be # used on a machine with PHP installed.## There are a number of user-based customizations# possible, most importantly the buoy station # from which to parse the buoy reports.# Please see below for details.################################# START OF USER CUSTOMIZATIONS ################################## Go to http://www.ndbc.noaa.gov/ and locate the station number# of the buoy for which you want to get data. Enter that number# below for $, and then provide whatever info you # want display for the station on the output page as $stationName.PRINT <<<HERE<html><head><link href="../bro_style2.css" rel="stylesheet" type="text/css"></head><body><table class="table_buoy" border="1" cellpadding="1" cellspacing="0"><tr><td>Bouy</td><td>Date</td><td>Time (PST)</td><td>Height (ft)</td><td>Dom. Period (sec)</td><td>Water Temp (F)</td><td>Air Temp (F)</td><td>Wind Dir.</td><td>Wind Speed (kts.)</td><td>Gust (kts.)</td></tr>HERE;## DREXLER NOTE: the sequence in the "$bouyName" array must be the same as in the "$bouy" array.$buoyName[0] = "Cape Elizabeth";$buoyName[1] = "Grays Harbor";$buoyName[2] = "Neah Bay";$buoyName[3] = "La Perouse Bank";$buoyName[4] = "Hein Bank";$buoy[0] = 46041;$buoy[1] = 46211;$buoy[2] = 46087;$buoy[3] = 46206;$buoy[4] = 46088;for($counter = 0; $counter < 5; $counter += 1){$stationNumber = $buoy[$counter];$stationName = $buoyName[$counter];$stationURL = "<a target=_blank class=grey-blue href=http://www.ndbc.noaa.gov/station_page.php?station=$buoy[$counter]>"."$buoyName[$counter]</a>";# This is the maximum number of readings to display of the buoy data.# If you only want to show the most current reading, leave this unchanged.$maxReadings = 1;# Set the GMT (Greenich Mean Time) offset for the location of the # buoy you are getting reports for. Pacific Standard Time is a -8 hour #offset from GMT; leave this unchanged if that is your desired timezone.$gmtOffset = -8;# 12-hour or 24-hour clock preference# For a 12-hour clock, set $clock = 0;# For a 24-hour clock, set $clock = 1;$clock = 0;# Date format choice.# For U.S.-style dates (MM-DD-YYYY), set $intlDateFormat = 0;# For international-style dates(DD-MM-YYYY), set $intlDateFormat = 1;$intlDateFormat = 0;# Choose metric or English measurements.# For English measurements, set $metric = 0;# For metric measurements, set $metric = 1;$metric = 0;# You can customize the phpBuoy output HTML table, if you want. # If you are using phpBuoy as an nested include file within an existing# table-based page layout, you can probably leave this as-is.## NOTE: Modify only the HTML code between the PRINT <<<HERE and HERE;!# Make sure that PRINT <<<HERE and HERE; remain.########################## END OF CUSTOMIZATIONS ########################################################################################################################### Really no need to tweak the code from this point down, unless you want to make it better! :) ################################################################################################## Location of remote backend file and local cache file# $backend = "http://www.surfimplement.com/include/46063.txt";$backend = "http://www.ndbc.noaa.gov/data/realtime2/".$buoy[$counter].".txt";$cache_file = "/tmp/buoydata.".$buoy[$counter].".cache";# Set timeout duration for establishing a connection# with NDBC's servers...if they're down, we don't want # the script to bomb in graceless fashion. The number# is in seconds.$timeout = 10;# Cache file time-based naming stuff$cache_time = 5; # Buoy readings are only updated every hour so cache the current reading until the next is available$time = split(" ", microtime());srand((double)microtime()*1000000);$cache_time_rnd = 300 - rand(0, 600);$maxReadings++; # Takes into account the first line of buoy data, which is just column headings like YYYY, DD, HH, ATMP, etc.$numLines = 0;if ( (!(file_exists($cache_file))) || ((filectime($cache_file) + $cache_time - $time[1]) + $cache_time_rnd < 0) || (!(filesize($cache_file))) ) { $url = parse_url($backend); $fp = fsockopen($url['host'], "80", &$errno, &$errstr, $timeout); if(!$fp) { echo "<tr><td>Buoy data currently unavailable - server appears down<br><br><br></td></tr></table>\n"; return; } else { $fpread = @fopen($backend, 'r'); socket_set_timeout($fpread, 3); if(!$fpread) { echo "<tr><td>Buoy data currently unavailable - file could not be loaded<br><br><br></td></tr></table>\n"; return; } else { $fpwrite = @fopen($cache_file, 'w'); if(!$fpwrite) { echo "<tr><td>Buoy data currently unavailable - could not read cache file<br><br><br></td></tr></table>\n\n"; return; } else { $line = fgets($fpread, 1024); while((!feof($fpread)) && ($numLines < $maxReadings)) { $line = eregi_replace("MM","N/A",$line); list($YYYY,$MM,$DD,$hh,$mm,$WD,$WSPD,$GST,$WVHT,$DPD,$APD,$MWD,$BARO,$ATMP,$WTMP,$DEWP,$VIS,$PTDY,$TIDE) = preg_split("/[\s,]+/", $line); # Format the date to MM-DD-YYYY or DD-MM-YYYY if($intlDateFormat == 0) { $formattedDate = $MM."-".$DD."-".$YYYY; } elseif($intlDateFormat != 0) { $formattedDate = $DD."-".$MM."-".$YYYY; } $hh = $hh + $gmtOffset; # Compensate for GMT timezone offset if($clock == 0) { if($hh < 1) { $hour = $hh + 12; # 12hour clock fix $ampm = "PM"; } elseif($hh > 12) { $hour = $hh - 12; # 12hour clock fix $ampm = "PM"; } elseif($hh == 12) { $hour = $hh; $ampm = "PM"; } else { $hour = $hh; $ampm = "AM"; } } elseif($clock != 0) { if($hh < 0) { $hour = $hh + 24; # 24hour clock fix } } # Metric-to-English conversion stuff if($metric == 0) { $windSpeed = "kts."; # "Knots" abbrev. $waveHeight = "ft."; # "Feet" abbrev. $temp = "F"; # "Fahrenheit" abbrev. if($WVHT != "N/A") { $WVHT = round($WVHT * 3.28, 1); # Convert wave height from feet to meters } if($WSPD != "N/A") { $WSPD = round($WSPD * 1.9425, 1); # Convert wind speed from m/s to knots } if($GST != "N/A") { $GST = round($GST * 1.9425, 1); # Convert gust speed from m/s to knots } if($ATMP != "N/A") { $ATMP = round(($ATMP * 1.8) + 32, 1); # Convert air temp from C to F } if($WTMP != "N/A") { $WTMP = round(($WTMP * 1.8) + 32, 1); # Convert water temp from C to F } } elseif($metric != 0) { $windSpeed = "m/s"; # "Meters/Second" abbrev. $waveHeight = "m"; # "Meters" abbrev. $temp = "C"; # "Celcius" abbrev. } # Wind direction conversion stuff if(($WD != "N/A") && ($WD != "WD")) { if(($WD >= 348 && $WD <= 360) || ($WD >= 0 && $WD < 11)) { $WDIR = "N"; } elseif($WD >= 326 && $WD < 348) { $WDIR = "NNW"; } elseif($WD >= 303 && $WD < 326) { $WDIR = "NW"; } elseif($WD >= 281 && $WD < 303) { $WDIR = "WNW"; } elseif($WD >= 258 && $WD < 281) { $WDIR = "W"; } elseif($WD >= 236 && $WD < 258) { $WDIR = "WSW"; } elseif($WD >= 213 && $WD < 236) { $WDIR = "SW"; } elseif($WD >= 191 && $WD < 213) { $WDIR = "SSW"; } elseif($WD >= 168 && $WD < 191) { $WDIR = "S"; } elseif($WD >= 146 && $WD < 168) { $WDIR = "SSE"; } elseif($WD >= 123 && $WD < 146) { $WDIR = "SE"; } elseif($WD >= 101 && $WD < 123) { $WDIR = "ESE"; } elseif($WD >= 78 && $WD < 101) { $WDIR = "E"; } elseif($WD >= 56 && $WD < 78) { $WDIR = "ENE"; } elseif($WD >= 33 && $WD < 56) { $WDIR = "NE"; } elseif($WD >= 11 && $WD < 33) { $WDIR = "NNE"; } else { $WDIR = "N/A"; } } else { $WDIR = "N/A"; } # Print out results of parsing if($numLines >= 1) { fputs($fpwrite, "<tr><td>$stationURL</td><td>$MM/$DD</td><td>$hour:$mm $ampm</td><td>$WVHT $waveHeight</td><td>$DPD sec.</td><td>$WTMPº$temp</td><td>$ATMPº$temp</td><td>$WD</td><td>$WSPD $windSpeed</td><td>$GST $windSpeed</td></tr>\n"); if($maxLines > 2) { fputs($fpwrite, "<tr><td> </td></tr>\n"); # Spacer break for multiple reading display } } $line = fgets($fpread, 1024); $numLines++; } } fclose($fpread); } fclose($fpwrite); } fclose($fp);}if (file_exists($cache_file)) { include($cache_file);}}PRINT <<<HERE</table>HERE;?>[/code] Link to comment https://forums.phpfreaks.com/topic/32801-solved-warning-socket_set_timeout/ Share on other sites More sharing options...
448191 Posted January 4, 2007 Share Posted January 4, 2007 Set stream timeout AFTER the connection is established:[code=php:0]if($fpread = fopen($backend, 'r')) { socket_set_timeout($fpread, 3); $line = fgets($fpread, 1024);}[/code]Appearantly the stream cannot be opened, remove @ to see why.If you want to set a timeout for the connecting, consider fsockopen. Link to comment https://forums.phpfreaks.com/topic/32801-solved-warning-socket_set_timeout/#findComment-152811 Share on other sites More sharing options...
drexnefex Posted January 5, 2007 Author Share Posted January 5, 2007 thanks for the reply.i figured out the problem.the script was parsing a series of txt files on a NOAA gov server. one of the files (the 1st in my array) isn't being served anymore due the Buoy that was recording wave data having been lost in the big winter storms we've been having out here.i eliminated that from my array, changed my counter and it worked! Link to comment https://forums.phpfreaks.com/topic/32801-solved-warning-socket_set_timeout/#findComment-153218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.