Reduce Bandwidth usage in cURL PHP

Viewed 217

I'm using basic cURL requests to fetch webpages in PHP, however these webpages are big in size and I'm limited in bandwidth usage.

Is there a way to reduce/optimize cURL data usage, for example using compression. I also heard that Brotli compression is the best, but I'm not sure how to use it.

1 Answers
$headers[] = "Accept-Encoding: gzip"; // tell the server you accept gzip
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch,CURLOPT_ENCODING , "gzip"); // tells curl to gunzip it automatically
$data = curl_exec($ch);

Not tried this with brotli, support will vary by software version which you didn't tell us about.

Related