Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

php - curl: How to limit size of GET?

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 appears to determine the size of a buffer that is reused until all of the content is retrieved.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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") );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...