jamesp0tter
Programmer
Hello all !
I'm developing a personal side project, and basically I need to get various bits of a remote file. I use the "Range" field in the HTTP request to accomplish this, and it's all working perfectly.
But, as I need various bits, initiating a connection, sending headers, getting response, and closing connection, just to repeat the process the next time seems a bit excessive and unnecessary.
So, basically, I was thinking opening the connection first, then use a function to get all the bits I need (separately, not every response in one result), and finally close it.
Problem is, I cannot figure out the correct headers to send !
Basically:
I need this to work, and don't go into an infinite loop like it's doing now.
Maybe a problem with the header "Connection" Maybe I need something to tell the socket it's being reused ?
Thanks in advance!
jamesp0tter
p.s.: sorry for my (sometimes) bad english
I'm developing a personal side project, and basically I need to get various bits of a remote file. I use the "Range" field in the HTTP request to accomplish this, and it's all working perfectly.
But, as I need various bits, initiating a connection, sending headers, getting response, and closing connection, just to repeat the process the next time seems a bit excessive and unnecessary.
So, basically, I was thinking opening the connection first, then use a function to get all the bits I need (separately, not every response in one result), and finally close it.
Problem is, I cannot figure out the correct headers to send !
Basically:
Code:
$fp = fsockopen($url['host'], 80, $errno, $errstr, 30);
if (!$fp)
die("Error: $errstr ($errno)");
function get_range($start,$len) {
global $fp,$url;
$start = intval($start);
$end = $start+intval($len)-1;
$resp = '';
$out = "GET {$url['path']}{$url['query']} HTTP/1.1\r\n";
$out .= "Range: bytes={$start}-{$end}\r\n";
$out .= "Host: {$url['host']}\r\n";
$out .= "Connection: Keep-Alive\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$resp .= fgets($fp, 128);
}
return substr($resp,strpos($resp,"\r\n\r\n")+4);
}
for ($i=0;$i<100;$i++)
// get_range(x,y) and do what i need with that specific response
fclose($fp);
Maybe a problem with the header "Connection" Maybe I need something to tell the socket it's being reused ?
Thanks in advance!
jamesp0tter
p.s.: sorry for my (sometimes) bad english