Blog

PHP Raw Curl

The regular PHP file functions sometimes are too slow and inadequate. In such cases you can use the raw "curl" command, which is faster and more adaptable.

1. Get the Headers of Remote File using cURL

/**
* Gets the headers using
* @param String $url
* @return String
*/
function rawCurlGetHeaders($url) {
    $cmd = 'curl ' . $url . ' --head';
    exec($cmd, $out);
    return $out;
}

2. Download Remote File using cURL

On an average server a 120MB file will take about 20secs. Impressive.

function rawCurlDownload($sourceUrl, $targetPath, $options = []) {
        $headers = isset($options['headers']) ? '--header "' . $options['headers'] . '"' : '';

        $cmd = 'curl -X GET ' . $headers . ' --output "' . $targetPath . '" -O ' . $sourceUrl . '';

        exec($cmd, $out);

        if (file_exists($targetPath) == false) {
            return false;
        }

        return true;
    }