Please can somebody tell me why this isn't returning the index page? It is technically supposed to, and if somebody can get it to work I can then use remote download resume functionality in my site. According to <link removed>, it is a HTTP 1.1 request that supposedly should work...I can't find any syntax errors! What's wrong?
[php]<?php
ob_end_clean();
ob_implicit_flush(TRUE);
$host = "www.example.com";
$url = "/index.html";
$port = 80;
$saveToFile = "index.html";
$nn = "\r\n";
$method = "GET";
$fp = @fsockopen($proxyHost ? $scheme.$proxyHost : $scheme.$host, $proxyPort ? $proxyPort : $port, $errno, $errstr, 15);
if (!$fp)
{
html_error("Couldn't connect to ".($proxyHost ? $proxyHost : $host)." at port ".($proxyPort ? $proxyPort : $port), 0);
}
if($errno || $errstr)
{
$lastError = $errstr;
print $errstr."<br>".$errno."<br>";
}
socket_set_timeout($fp, 120);
if ($saveToFile)
{
if ($proxy)
{
echo "<p>Connected to proxy: <b>".$proxyHost."</b> at port <b>".$proxyPort."</b>...<br>\n";
echo "GET: <b>".$host.$url."</b>...<br>\n";
}
else
{
echo "<p>Connected to: <b>".$host."</b> at port <b>".$port."</b>...<br>";
}
}
if ($saveToFile)
{
do {
$redirect = "";
$request =
"HEAD ".str_replace(" ", "%20", $url)." HTTP/1.1".$nn.
"Host: ".$host.$nn.
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MEGAUPLOAD 1.0; Alexa Toolbar)".$nn.
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5".$nn.
"Accept-Language: en-en,en;q=0.5".$nn.
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7".$nn.
"Pragma: no-cache".$nn.
"Cache-Control: no-cache".$nn.
"Keep-Alive: 300".$nn.
"Connection: Keep-Alive".$nn.$nn;
fwrite($fp, $request);
//$fs = fopen("debug.txt", "wb");
//fwrite($fs, $request);
$head = "";
while(!feof($fp))
{
$head.= fgets($fp, 128);
}
print "<br>HEAD: ".$head.$nn;
if (stristr($head, "Location:"))
{
$redirect = trim(cut_str($head, "Location:", "\n"));
$rUrl = parse_url($redirect);
$scheme = $rUrl["scheme"] == "https" ? "ssl://" : "";
$port = $rUrl["port"] ? $rUrl["port"] : ($scheme == "ssl://" ? 443 : 80);
$host = $rUrl["host"];
$url = $rUrl["path"].($rUrl["query"] ? "?".$rUrl["query"] : "");
}
if (stristr($head, "Content-Disposition:"))
{
$Content = trim(cut_str($head, "Content-Disposition:", "\n"))."\n";
if (stristr($Content, "filename="))
{
$FileName = trim(trim(trim(cut_str($Content, "filename=", "\n")), ";"), '"');
$saveToFile = dirname($saveToFile).PATH_SPLITTER.basename($FileN ame);
}
}
/*
if (@file_exists($saveToFile))
{
if (is_numeric($filesize = @filesize($saveToFile)))
{
$Resume["use"] = TRUE;
$Resume["from"] = intval($filesize);
}
}
*/
} while (!empty($redirect));
}
$request =
$method." ".str_replace(" ", "%20", $url)." HTTP/1.1".$nn.
"Host: ".$host.$nn.
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MEGAUPLOAD 1.0; Alexa Toolbar)".$nn.
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5".$nn.
"Accept-Language: en-en,en;q=0.5".$nn.
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7".$nn.
"Pragma: no-cache".$nn.
"Cache-Control: no-cache".$nn.
($Resume["use"] === TRUE ? "Range: bytes=".$Resume["from"]."-".$nn : "").
$http_auth.
$proxyauth.
$referer.
$cookies.
"Keep-Alive: 300".$nn.
"Connection: Keep-Alive".$nn.
$content_tl.$nn.$postdata;
//fwrite($fs, $request);
//fclose($fs);
fwrite($fp, $request);
while(!feof($fp))
{
$data.= fgets($fp, 128);
}
print "<br>GET: ".$data.$nn;
fclose($fp);
function cut_str($str, $left, $right)
{
$str = substr(stristr($str, $left), strlen($left));
$leftLen = strlen(stristr($str, $right));
$leftLen = $leftLen ? -($leftLen) : strlen($str);
$str = substr($str, 0, $leftLen);
return $str;
}
?>[/php]