Optimal Telelicious Coding From AK To CT
 
« sfThumbnailCachePlugin - Cache thumbnails?
How To Control iTunes remotely with SSH »
 

Curl Multi Php Download How-To

This article will show you an example of how to download multiple files concurrently using the curl_multi commands in PHP 5.While php itself doesn’t support multi threading and concurrency, libcurl does and php allows us to download multiple filesat the same time from php.

// Files to download
$urls = array('http://static.scribd.com/docs/cdbwpohq0ayey.pdf',
              'http://static.scribd.com/docs/8wyxlxfufftas.pdf',
              'http://static.scribd.com/docs/9q29bbglnc2gk.pdf',);

// Path to save files in
$save_path = '/tmp';

$multi_handle = curl_multi_init();
$file_pointers = array();
$curl_handles = array();

// Add curl multi handles, one per file we don't already have
foreach ($urls as $key => $url) {
  $file = $save_path.'/'.basename($url);
  if(!is_file($file)){
    $curl_handles[$key]=curl_init($url);
    $file_pointers[$key]=fopen ($file, "w");
    curl_setopt ($curl_handles[$key], CURLOPT_FILE,           $file_pointers[$key]);
    curl_setopt ($curl_handles[$key], CURLOPT_HEADER ,        0);
    curl_setopt ($curl_handles[$key], CURLOPT_CONNECTTIMEOUT, 60);
    curl_multi_add_handle ($multi_handle,$curl_handles[$key]);
  }
}

// Download the files
do {
  curl_multi_exec($multi_handle,$running);
}
while($running > 0);

// Free up objects
foreach ($urls as $key => $url) {
  curl_multi_remove_handle($multi_handle,$curl_handles[$key]);
  curl_close($curl_handles[$key]);
  fclose ($file_pointers[$key]);
}
curl_multi_close($multi_handle);

Tags: ,

Leave a Reply