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?

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.