Decoding Gzip-Compressed Web Pages Retrieved via cURL in PHP
When retrieving gzipped web pages through cURL, the raw compressed data is often returned as the response. To properly decode this data in PHP, various methods can be employed.
One approach involves writing the content to a temporary file and using the gzopen, gzread, and gzclose functions to decompress it. However, a more efficient solution is available through cURL's auto-encoding feature.
Auto-Encoding in cURL
By setting the CURLOPT_ENCODING option to an empty string or 'gzip', cURL will automatically enable auto-encoding mode. In this mode:
Setting the Encoding Option
To enable auto-encoding, use the following command:
<code class="php">curl_setopt($ch, CURLOPT_ENCODING , ''); // Activates 'auto' mode</code>
Alternatively, to force gzip encoding in the request header, use:
<code class="php">curl_setopt($ch, CURLOPT_ENCODING , 'gzip');</code>
Conclusion
Auto-encoding is a convenient and efficient way to decode gzipped web pages retrieved via cURL in PHP. It eliminates the need for manual file handling and provides seamless decoding without sacrificing performance.
The above is the detailed content of How to Decode Gzip-Compressed Web Pages Retrieved via cURL in PHP. For more information, please follow other related articles on the PHP Chinese website!