Using the least RAM with curl_exec
Could you please tell me which code samples uses the least RAM? Here are my two examples:
$ch = curl_init();
foreach ($URLS as $u开发者_如何学JAVArl){
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url.'&no_cache');
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
}
// close cURL resource, and free up system resources
curl_close($ch);
or
foreach ($URLS as $url){
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url.'&no_cache');
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
curl_close($ch);
}
// close cURL resource, and free up system resources
First one has lighter overhead, as you only instantiate the curl object once, but if curl has any leaks in it, and you're fetching a large-ish number of URLs, you could run out of memory.
Usually I only invoke a new curl object if the next url to fetch has too many differences in settings than the old curl. Easier to start with a default setup and make changes from that than try to "undo" the conflicting settings from the previous run.
精彩评论