PHP CURL Download Remote URL to File

Posted on November 3, 2018 at 5:53 pm

Simple PHP snippet to download a remote URL to a file:

function curl_download_to_file( $url, $file )
{
    $fp = fopen($file, 'w+');
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt ($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1");
    curl_setopt ($curl, CURLOPT_HEADER, 0);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($curl, CURLOPT_FILE, $fp);
    curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_exec ($curl);
    curl_close ($curl);
    fclose($fp);
}

Receive updates via email

Other Posts

Updated Posts