Jump to content

[SOLVED] Grabbing/reading a text file from another server


policosmos

Recommended Posts

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&#92;r&#92;n"; 
$request .= "Host: $host&#92;r&#92;n"; 
$request .= "Referer: $host&#92;r&#92;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?

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&#38;#92;r&#38;#92;n"; 
$request .= "Host: $host&#38;#92;r&#38;#92;n"; 
$request .= "Referer: $host&#38;#92;r&#38;#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";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.