Skip to content

Instantly share code, notes, and snippets.

@nokimaro
Last active August 11, 2020 11:12
Show Gist options
  • Save nokimaro/18a24adf65125a1e1816 to your computer and use it in GitHub Desktop.
Save nokimaro/18a24adf65125a1e1816 to your computer and use it in GitHub Desktop.
PHP Curl functions
<?php
//via http://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl
function curl_copy($from = '', $to = '', $proxy = '')
{
if(empty($from) || empty($to))
{
return false;
}
$has_error = false;
$fp = fopen ($to, 'w+');//This is the file where we save the information
$ch = curl_init($from);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 3600); //1 hour
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//чтобы избежать зависания при скачке (на самом деле отключает буфферизацию TCP)
//curl_setopt($ch, CURLOPT_TCP_NODELAY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_VERBOSE, true);
if(!empty($proxy))
{
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
if(false === $result = curl_exec($ch))
{
$has_error = true;
}
if(curl_errno($ch) != 0)
{
$has_error = true;
error_log('Curl error ('.curl_errno($ch).'): '.curl_error($ch));
echo 'Curl error '.(curl_errno($ch)).': '.curl_error($ch);
}
$curlVersion = curl_version();
$result_str = (string)$result;
extract(curl_getinfo($ch));
$metrics = <<<EOD
URL....: $url
Code...: $http_code ($redirect_count redirect(s) in $redirect_time secs)
Content: $content_type Size: $download_content_length (Own: $size_download) Filetime: $filetime
Time...: $total_time Start @ $starttransfer_time (DNS: $namelookup_time Connect: $connect_time Request: $pretransfer_time)
Speed..: Down: $speed_download (avg.) Up: $speed_upload (avg.)
Curl...: v{$curlVersion['version']}
Bool...: $result_str
EOD;
if($download_content_length != $size_download)
{
echo $metrics;
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n ".$verboseLog."\n";
$has_error = true;
}
else
{
echo $download_content_length.' == '.$size_download.' '.$url."\n";
}
curl_close($ch);
fclose($fp);
if($has_error)
{
return false;
}
return true;
}
<?php
//via http://php.net/manual/en/function.get-headers.php#90434
function get_headers_curl($url, $proxy = '')
{
$return = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
if(!empty($proxy))
{
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$r = curl_exec($ch);
$ex = explode("\n", $r);
foreach($ex as $val)
{
$val = trim($val);
if(!empty($val))
{
if(stripos($val, ': ') !== false)
{
$exp = explode(': ', $val, 2);
$return[$exp[0]] = $exp[1];
}
else
{
$return[] = $val;
}
}
}
return $return;
}
<?php
//via http://stackoverflow.com/questions/13958303/curl-download-progress-in-php
echo "<pre>";
echo "Loading ...";
ob_flush();
flush();
$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;
ob_flush();
flush();
sleep(1); // just to see effect
}
echo "Done";
ob_flush();
flush();
<?php
//via http://stackoverflow.com/questions/3757071/php-debugging-curl
//via http://stackoverflow.com/a/9571305/367456
//after curl_exec
$curlVersion = curl_version();
extract(curl_getinfo($curlHandle));
$metrics = <<<EOD
URL....: $url
Code...: $http_code ($redirect_count redirect(s) in $redirect_time secs)
Content: $content_type Size: $download_content_length (Own: $size_download) Filetime: $filetime
Time...: $total_time Start @ $starttransfer_time (DNS: $namelookup_time Connect: $connect_time Request: $pretransfer_time)
Speed..: Down: $speed_download (avg.) Up: $speed_upload (avg.)
Curl...: v{$curlVersion['version']}
EOD;
<?php
//via http://stackoverflow.com/questions/3757071/php-debugging-curl
// CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR,
// or the file specified using CURLOPT_STDERR.
curl_setopt($curlhandle, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($curlHandle, CURLOPT_STDERR, $verbose);
if (false === $result = curl_exec($curlHandle)) {
printf("Curl error (#%d): %s\n", curl_errno($curlHandle),
curl_error($curlHandle));
}
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose info:\n".$verboseLog."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment