Home >Backend Development >PHP Tutorial >How to Decode a Gzipped Web Page Retrieved via cURL in PHP?
Decoding a Gzipped Web Page Retrieved via cURL in PHP
When retrieving a gzipped web page using cURL, extracting the actual content can be challenging if it appears in raw form. PHP offers a better solution than manually decompressing the data through a temporary file.
Solution:
The key lies in configuring cURL's "auto encoding" mode. This enables cURL to communicate its support for different encoding methods (via the Accept-Encoding header) and automatically handle the decompression process. To activate this mode, use the following code:
<code class="php">// Allow cURL to use gzip compression or any other supported encoding // A blank string activates 'auto' mode curl_setopt($ch, CURLOPT_ENCODING, '');</code>
Alternatively, to specify a specific encoding (gzip only), use this command:
<code class="php">// Allow cURL to use gzip compression curl_setopt($ch, CURLOPT_ENCODING, 'gzip');</code>
This simple adjustment will let cURL automatically decompress the gzipped web page, providing you with the decoded content directly. Refer to the PHP documentation for more information on curl_setopt.
The above is the detailed content of How to Decode a Gzipped Web Page Retrieved via cURL in PHP?. For more information, please follow other related articles on the PHP Chinese website!