Hi paul, this can be a real bitch to get working, but I'll give you a hand anyway 😉
What you want to be doing is using fsockopen() like you suggested. The theory is this.
Open a socket to the web server.
Send the HTTP headers to get the server to send the page.
Read the data the page is returning.
Close the socket.
Write said data to a file.
This is how it's done in practice 🙂
<?php
// We set Remote variables here
$sHost = "yourdomain.com"; // Host to open the socket to
$iPort = 80; // Port to open the socket on
$sFilePath = "/path/to/file"; // Path to the file you want to retrieve
$iTimeOut = 10; // Timeout of the socket
// We create the headers here, so we keep the socket connection open for the shortest time possible
$sHeaders = "GET $sFilePath HTTP/1.1\r\n"; // This is required
$sHeaders .= "Host: $sHost\r\n"; // This is required
$sHeaders .= "Referer: ".$_SERVER['REQUEST_URI']."\r\n"; // Not required, but handy
$sHeaders .= "User-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n"; // Not required
$sHeaders .= "\r\n"; // You must have an extra CR/LF at the bottom
$sLocalFile = "/path/to/local/file"; // Kind of obvious, path to the local file to write to
// Connect to the webserver and error check it.
if(($fp = @fsockopen($sHost, $iPort, $errno, $errmsg, $iTimeOut)) === false) {
die("Could not open socket!");
}
// Write our headers to the socket, if it fails, die and give an error message
if((fwrite($fp, $sHeaders)) === false) {
die("Could not write to socket!");
}
// Get the status of the socket, used in the next bit
$aSocketStatus = socket_get_status($fp);
// Read the data that the web server returns
$sData = fread($fp, $aSocketStatus['unread_bytes']);
// Close the socket
fclose($fp);
// Open the local file for writing
if(($fp = @fopen($sLocalFile, "w")) === false) {
die("Could not open local file!");
}
// Write the data to the local file
fputs($fp, $sData);
// Close the local file!
fclose($fp);
// Echo Success!
echo "It's ALIVE!";
?>