I want to retrieve the first 10k bytes from a URL with curl (using PHP in my case). Is there a way to specify this? I thought CURLOPT_BUFFERSIZE would do this, but it just a
CURLOPT_RANGE appears to not work in PHP although it's there. At least it didn't have an impact when I tried to use it and a google search will reveal many messages of the same.
What about this:
// 1-10240 is range of downloaded bytes (10 kb = 10240 byte)
curl_setopt($ch, CURLOPT_RANGE,"1-10240");
$html='';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($DownloadSize, $Downloaded, $UploadSize, $Uploaded){ return ($Downloaded > 10240) ? 1 : 0;});
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_function');
curl_exec($ch);
curl_close($ch);
echo $html;
function write_function($handle, $data) {
global $html;
$html .= $data;
if (strlen($html) > 10240) {
return 0;
}
else
return strlen($data);
}
If you use fread instead of curl, although I prefer curl, you can specify the size of the data you want to receive, for example:
$fp = @fopen($url, "r") ;
$data = "" ;
if($fp) {
while (!feof($fp)) {
$data .= fread($fp, $size) ;
}
fclose($fp) ;
This is how i do it in c++
int offset = 0;
int size = 10*1024;
char range[256];
curl_slist_s *pHeaders = NULL;
snprintf(range, 256, "Range: bytes=%d-%d", offset, offset+size-1);
pHeaders = curl_slist_append(pHeaders, range);
curl_easy_setopt(pCurlHandle, CURLOPT_HTTPHEADER, pHeaders);
curl_slist_free_all(pHeaders);
pHeaders = NULL;
Edit: Just found out you meant in php. Ill see if i can find out how to port it.
Think this should work in php:
$offset = 0;
$size = 10*1024;
$a = $offset;
$b = $offset + $size-1;
curl_easy_setopt(curlHandle, CURLOPT_HTTPHEADER, array("Range: bytes=$a-$b") );