Uncompress a Gzip File from Curl, on PHP

Uncompress a gzip file from CURL, on php

Use gzdecode:

<?php
$c = file_get_contents("http://torcache.com/" .
"torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
echo gzdecode($c);

gives


d8:announce42:http://tracker.openbittorrent.com/announce13:announce-listll42
...

php curl response show gzip or encoded data

In header i allowed gzip and deflate only and removed br and it worked for me. So instead of this $header[] = 'Accept-Encoding: gzip, deflate, br'; i used $header[] = 'Accept-Encoding: gzip, deflate';

Thanks for help every one.

Requesting a GZIP'ed page and processing with cURL and PHP

You can request a gzipped encoding with curl_setopt, like this:

curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); 

You can then decompress the content with gzdecode like this:

$response = gzdecode($response);

Problems getting gzipped XML files with curl PHP

Not sure why, but none of the other answers worked for me in the end. zlib was installed on the server, but the gzdecode() function was not defined in the library, and the gzuncompress gave me errors, as did compress.zlib://. They might work for you so, give them a try as well.

If you need to check if zlib is installed this stackoverflow answer or this answer can help. They provide this script:

<?php

echo phpversion().", ";

if (function_exists("gzdecode")) {
echo "gzdecode OK, ";
} else {
echo "gzdecode no OK, ";
}

if (extension_loaded('zlib')) {
echo "zlib extension loaded ";
} else {
echo "zlib extension not loaded ";
}

?>

This site gives another script that shows what zlib function are installed:

var_dump(get_extension_funcs('zlib'));

SOLUTION!!! These 2 functions did the trick for me. Just curl or use file_get_contents to grab the xml file, then use this script:

$xmlcontent = gzinflate(substr($xmlcontent,10,-8));

OR use this script to grab the xml file and get the contents (see more here):

$zd = gzopen($filename,"r");
$contents = gzread($zd,$fileSize);
gzclose($zd);

Thanks to all who helped me get this answer. Hope this helps someone else!

Decode gzipped web page retrieved via cURL in PHP

I use curl and:

curl_setopt($ch, CURLOPT_ENCODING , "gzip");

Is there any way to get curl to decompress a response without sending the Accept headers in the request?

Probably the easiest thing to do is just use gunzip to do it:

curl -sH 'Accept-encoding: gzip' http://example.com/ | gunzip -

Or there's also --compressed, which curl will decompress (I believe) since it knows the response is compressed. But, not sure if that meets your needs.

How can I extract or uncompress gzip file using php?

Try this found here

//This input should be from somewhere else, hard-coded in this example
$file_name = '2013-07-16.dump.gz';

// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name);

// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');

// Keep repeating until the end of the input file
while (!gzeof($file)) {
// Read buffer-size bytes
// Both fwrite and gzread and binary-safe
fwrite($out_file, gzread($file, $buffer_size));
}

// Files are done, close files
fclose($out_file);
gzclose($file);


Related Topics



Leave a reply



Submit