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
How to Check for a Specific Type of Object in PHP
Differencebetween ' and " in PHP
Should Lock_Ex on Both Read & Write Be Atomic
How to Stop PHP Imagick Auto-Rotating Images Based on Exif 'Orientation' Data
How to Maintain Session in Curl in PHP
Curl Requires Curlopt_Ssl_Verifypeer=False
How to Search Through a JSON Array in PHP
How to Send Cookies with File_Get_Contents
Php: Check Who Had Read Sent Email
Warning: "Continue" Targeting Switch Is Equivalent to "Break". Did You Mean to Use "Continue 2"
Sending Correct File Size with PHP Download Script
Where Do I Put a Database Query in MVC
How to Bulk Update MySQL Data with One Query
Multi-Byte Safe Wordwrap() Function for Utf-8
How to Create a New Joomla User Account from Within a Script
Warning: MySQLi_Real_Escape_String() Expects Exactly 2 Parameters, 1 Given... What I Do Wrong