I'm using PHP4.2.1 on Win32, Apache 1.3. I've got some code that I'm using to open a connection using fsockopen and then grab a file (this is borrowed and modified from someone's code that I found on these forums):
function openURL($host,$port=80,$page,$user_agent='',$user='',$pass='',$referer='',$withHeaders=FALSE) {
// Open a connection to the webserver
$fp = fsockopen($host,$port,$errno,$errstr,10);
if (!$fp) {
return false;
}
// Request the page
fputs($fp,"GET $page HTTP/1.0\r\n");
if (!$user_agent) {
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461; rant.idlewords.net)';
}
fputs($fp,"User-agent: $user_agent\r\n");
if ($user) {
fputs($fp,"Authorization: BASIC ".base64_encode($user.':'.$pass)."\r\n");
}
if ($referer) {
fputs($fp,"Referer: $referer\r\n");
}
fputs($fp,"\r\n");
// Read the response
while(!feof($fp)) {
$document .= fgets($fp,2048);
}
// Close the Connection
fclose($fp);
if (!$withheaders) {
$pos = strpos($document, "<!doctype");
$document = substr($document,$pos);
}
return $document;
}
Generally the code works. However sometimes instead of returning the file intact, it replaces random newlines with (supposedly) ASCII char 42 (PHP is telling me this is a backslash - \ - though I can't prove it as it appears as a little block in my text editor). Any idea why that would happen?