Skip to content

Instantly share code, notes, and snippets.

@dausruddin
Created September 19, 2016 01:53
Show Gist options
  • Save dausruddin/fe68487ebcddf931b4cf2c09054594cc to your computer and use it in GitHub Desktop.
Save dausruddin/fe68487ebcddf931b4cf2c09054594cc to your computer and use it in GitHub Desktop.
Curl with progress. Taken from stackoverflow (forgot the link to source)
<?php
echo "<pre>";
echo "Loading ...<br>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
//curl_setopt($ch, CURLOPT_BUFFERSIZE,128);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);
function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
if($download_size > 0)
echo $downloaded / $download_size * 100;
echo "<br>";
sleep(1); // just to see effect
}
echo "Done";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment