Home  >  Article  >  Backend Development  >  How to Decode a Gzipped Web Page Retrieved via cURL in PHP?

How to Decode a Gzipped Web Page Retrieved via cURL in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-24 04:44:02899browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn