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

Susan Sarandon
Release: 2024-10-24 04:44:02
Original
898 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!