B
B
BoriHagen2020-11-13 21:57:49
Character encoding
BoriHagen, 2020-11-13 21:57:49

How to decrypt gzip archive from file_get_contents?

header('Content-type: text/html; charset=utf-8');
  header('Content-Encoding: gzip');

  $url = "https://www.rusprofile.ru/id/11597949";
  $file = file_get_contents($url);

        echo($file);


Conclusion:
5faed6b02c619562585751.jpeg

I can't figure out how to decrypt the file. My php file is UTF-8 encoded without BOM. File encoding from site Utf-8, gzip compression, deflate, br.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Надим Закиров, 2020-11-14
@BoriHagen

Укажите в заголовках запроса, чтобы целевой сайт свой ответ отдавал без сжатия:

<?php

header('Content-type: text/html; charset=utf-8');

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-Encoding: identity\r\n".
              "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36"
  )
);

$context = stream_context_create($opts);

$html = file_get_contents('https://www.rusprofile.ru/id/11597949', false, $context);

echo $html;

K
ksnk, 2020-11-14
@ksnk

В таком случае удобнее открывать гзипы с помошью curl. Если поставить параметр CURLOPT_ENCODING = 'gzip', то передаваемый в гзипе контент сам распакуется. если надо.

function getcontents($url){
    if(filter_var($url,FILTER_VALIDATE_URL)) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        // ssl?
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    return file_get_contents($url);
}


 echo getcontents('https://www.rusprofile.ru/id/11597949');

Антон Шаманов, 2020-11-13
@SilenceOfWinter

gzdecode — Decodes a gzip compressed string

O
omegastripes, 2021-01-24
@omegastripes

Мне помог вот такой код:

$url = 'https://www.kinopoisk.ru/';
$options = [
  'http' => [
    'method' => 'GET',
    'header' => 'user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
  ],
  'ssl' => [
    'verify_peer' => false,
    'verify_peer_name' => false
  ]
];
$context = stream_context_create($options);
$resp = file_get_contents($url, false, $context);
foreach($http_response_header as $c => $h) {
  if(stristr($h, 'content-encoding') and stristr($h, 'gzip')) {
    $resp = gzinflate(substr($resp, 10, -8));
    break;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question