PHP Curl - 返回的html都乱了 [英] PHP Curl - returned html all messed up

查看:103
本文介绍了PHP Curl - 返回的html都乱了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Curl获取一些关于网站文章的内容,我正在做如下操作:

I'm trying to fetch some contents about articles in a website via Curl, which I'm doing as follows :

    $url = 'http://lisboacity.olx.pt/oportunidade-pastor-alemao-7-meses-com-lop-iid-432402267';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322');
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;

但是,正如你所看到的,结果类似于:

However, as you can see, the result is similar to this :

‹ÜW_oÛ6N€~F{hD’å?±"ØÜÄm‚&qZ;-Ö¢0h‰–™R¢JR¶Óapcèð}*ö²§)%Vœ&YÚ·†eêî~¿;òx<6×ö{{ƒ_N»èp|„NÏžî!ËvÝו=×Ýìg‚ªSòÐ@àXREyŒ™ëvO,dM"Jv\w6›9³ŠÃEè^º±ªË8—Ä TµW›ú•~À#" #mj")¶¬=++{p‘ùÙ¨e)2Wmù,$Q­Ã~Ïn4jÛ¶g!÷.¨#‡)‹p‰26«>Í–M·.ƒTŽ8ö©ºp8›;‰r-¤°ÕJÂÆ£¢Š‘v/áB¥1 p@ÖN±T\ #Ñ'Žê("’H ŽÐQïÔ…#ƒ:10•(à £¨Ï%¼D]øá??ñ¦›d‘Å8"-+ Ò4Ñ3_ç:åÓÏÁ†ð’\[‘8]ÿÑëÎà zÕ;AOûý½ƒÞÓþA÷ðxíÑê£Uã»ôvS_pB"M ’aÙq€AŠX"øNa¦bx’;hŸÊäoCÃ0þjB3C@ Rå"™0Ãz€cž&ü{æäjúô '&äö'¤åUªõZ½î5êÀd2Ñø=„µ,Ç<†bÛìž3èGöØj±Ð{9Ø; ýÞÉ«,Æ]©.‘îO!Åb~–Á2 !°'uåÊj_Êÿ„œ=†žç;Æ$"Ó-3–­

我也尝试用PHP的 DomDocument 类加载url内容,结果相同。

I've also tried to load the url contents with PHP's DomDocument class with the same result.

可能是什么原因?提前感谢!

What could be causing this? Thanks in advance!

推荐答案

输出是gzip编码的。尝试此选项以解压缩内容:

The output is gzip encoded. Try this option to decompress the content:


curl_setopt($ ch,CURLOPT_ENCODING,'');

curl_setopt($ch, CURLOPT_ENCODING, '');

CURLOPT_ENCODING:Accept-Encoding:头的内容。这使得能够解码响应。支持的编码为identity,deflate和gzip。如果设置了空字符串,则会发送包含所有支持的编码类型的标题。

CURLOPT_ENCODING: The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent.

http://us2.php.net/curl_setopt

这篇关于PHP Curl - 返回的html都乱了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆