您的购物车目前是空的!
[title]gzip压缩传输导致乱码[/title]
今天在采集京东的时候发现返回的数据是乱码,网上说可能和压缩有关,看了一下京东的头信息的确进行gzip加密,好吧,那就解压吧
$return = gzdecode($return); //将return的字符进行解码
另一种解决办法
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
[title]页面编码问题[/title]
mb_convert_encoding($str, 'utf-8', 'GBK,UTF-8,ASCII');
[title]完整代码示例[/title]
// curl 伪造agent抓取页面 function http_get($URL,$key='baidu') { $agent = Flight::get('flight.spider_agent')[$key]; if( !$agent ){ return false; } $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_USERAGENT, $agent); curl_setopt($c, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate')); curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');//这个是解释gzip内容................. curl_setopt($c, CURLOPT_URL, $URL); curl_setopt($c, CURLOPT_TIMEOUT,2); $contents = curl_exec($c); $contents = mb_convert_encoding($contents, 'utf-8', 'GBK,UTF-8,ASCII'); $httpCode = curl_getinfo($c,CURLINFO_HTTP_CODE); curl_close($c); return ['data'=>$contents,'http_code'=>$httpCode]; }