policosmos Posted July 15, 2007 Share Posted July 15, 2007 I'm working on an idea to grab data from a text file with a bunch of space separated values that contains data from buoy reports at NOAA. Unlike their weather feeds, they don't serve up XML buoy data. Here's the file I'm trying to grab: http://www.ndbc.noaa.gov/data/realtime2/41004.spec I've tried several methods, and the one that seemed to have the most promise tried to make a socket connection: <?php $host = "http://www.ndbc.noaa.gov"; $page = "/data/realtime2/41004.spec"; $fp = fsockopen($host, 80, $errno, $errdesc) or die("Connection to $host failed"); $request = "GET $page HTTP/1.0\r\n"; $request .= "Host: $host\r\n"; $request .= "Referer: $host\r\n"; fputs($fp, $request); while(!feof($fp)){ $page[] = fgets($fp, 1024); } fclose($fp); for($i=0; $i < count($page); $i++){ $data .= $page[$i]; } ?> But all I get is this: Connection to http://www.ndbc.noaa.gov failed I've been flailing around for a few hours now, but I'm out of new ideas. Once I get the data, I'm planning to throw it all into an array and/or MySQL db and only grab it once per hour max. And go from there. Any ideas? Quote Link to comment Share on other sites More sharing options...
infid3l Posted July 15, 2007 Share Posted July 15, 2007 You can just do: $html = file_get_contents("http://www.ndbc.noaa.gov/data/realtime2/41004.spec"); or: $html = implode('', file('http://www.ndbc.noaa.gov/data/realtime2/41004.spec')); btw your problem was you had the "http://" in the host, which fsockopen() doesn't like. and, $request = "GET $page HTTP/1.0&#92;r&#92;n"; $request .= "Host: $host&#92;r&#92;n"; $request .= "Referer: $host&#92;r&#92;n"; should have been: $request = "GET $page HTTP/1.1\r\n"; $request .= "Host: $host\r\n"; $request .= "Connection: Close\r\n\r\n"; Quote Link to comment Share on other sites More sharing options...
policosmos Posted July 15, 2007 Author Share Posted July 15, 2007 Thanks! That was so easy. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.