PHP에서 CURL을 통해 검색된 Decode Gzipped 웹 페이지
문제
Curl을 통해 Gzipped 웹 페이지를 검색하고 있지만 검색된 컨텐츠를 브라우저에 출력하면 원시 GZIPED 데이터를 얻습니다. PHP에서 데이터를 디코딩하려면 어떻게해야합니까?
내가 찾은 방법 중 하나는 컨텐츠를 TMP 파일에 작성한 다음 ...
$f = gzopen($filename,"r");
$content = gzread($filename,250000);
gzclose($f);
....하지만 남자, 더 나은 방법이 있어야합니다.
편집 : 이것은 파일이 아니지만 웹 서버에서 반환 한 Gzipped HTML 페이지입니다.
해결책
나는 컬을 사용하고 :
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
다른 팁
의견에 제안 된 몇 가지 해결책이 있습니다. gzdecode의 PHP 페이지.
다재다능한 Gunzip 기능 :
function gunzip($zipped) { $offset = 0; if (substr($zipped,0,2) == "\x1f\x8b") $offset = 2; if (substr($zipped,$offset,1) == "\x08") { # file_put_contents("tmp.gz", substr($zipped, $offset - 2)); return gzinflate(substr($zipped, $offset + 8)); } return "Unknown Format"; }
CURL과 기능을 통합하는 예 :
$headers_enabled = 1; curl_setopt($c, CURLOPT_HEADER, $headers_enabled) $ret = curl_exec($c); if ($headers_enabled) { # file_put_contents("preungzip.html", $ret); $sections = explode("\x0d\x0a\x0d\x0a", $ret, 2); while (!strncmp($sections[1], 'HTTP/', 5)) { $sections = explode("\x0d\x0a\x0d\x0a", $sections[1], 2); } $headers = $sections[0]; $data = $sections[1]; if (preg_match('/^Content-Encoding: gzip/mi', $headers)) { printf("gzip header found\n"); return gunzip($data); } } return $ret;
제휴하지 않습니다 StackOverflow