PHP Curl - 返回的 html 全部搞砸了

标签 php html curl

我正在尝试通过 Curl 获取网站中文章的一些内容,我正在这样做:

    $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;

但是,如您所见,结果与此类似:

‹Ü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 内容,结果相同。

这可能是什么原因造成的?提前致谢!

最佳答案

输出是 gzip 编码的。试试这个选项来解压缩内容:

curl_setopt($ch, CURLOPT_ENCODING, '');

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 全部搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488111/

相关文章:

php - 如何获取当月的所有星期五

php - 使用 PHP SQL 在数据库中查找特定值

php - 四舍五入到最接近的 50 美分

html - 使用 HTML 和 CSS 在第一个打印页面上显示不同的页眉

c++ - curl 停止在 Linux 上使用多个连接进行下载

php - jQuery 文件上传和获取 $_Files

html - 相同高度的框(div)

Java-如何在 HTML 文件中声明属性?

python - 无法使用 python-requests 发布文件+数据

linux - 松弛 curl 不起作用