1. 程式人生 > >curl解壓gzip頁面gzcompress內容

curl解壓gzip頁面gzcompress內容

-a con 解壓 nec -type contents linu array android

$headers = array(
//"Content-type:application/json;charset=‘utf-8‘",
"Cache-Control:no-cache",
"Pragma:no-cache",
//"accept-charset:utf-8",
"Accept-Encoding:gzip",
"User-Agent:Dalvik/2.1.0 (Linux; U; Android 5.1.1;Build/LMY47V)",
"Host:www.domain.cn",
//‘Transfer-Encoding:chunked‘, 壓縮不能用這項
//"Content-Type:application/x-www-form-urlencoded",
//"Connection:Keep-Alive",
);

/*
* 可寫文件
$sourceFile="tmp.txt";
$fp = fopen($sourceFile, "w");
*/

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, ‘gzip‘); //curl解壓gzip頁面內容
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_HTTPGET, true);
//curl_setopt($curl, CURLOPT_FILE, $fp);
$data = curl_exec($curl);
curl_close($curl);

//file_put_contents(‘tmpdata.txt‘,$data);
echo bin2hex($data); //可查看下16進制
$ret = gzuncompress($data);

print_r(json_decode($ret,true)

curl解壓gzip頁面gzcompress內容