【发布时间】:2012-10-26 13:30:10
【问题描述】:
我在 MacOS 上通过终端使用 PHP 命令 file_get_contents() 以及 wget 和 curl 时遇到了问题。
问题在于,对于某些网站,我只能得到一个 HTML 文件或奇怪符号的文本。我猜它必须与编码有关,但我没有找到任何东西。
为什么会发生这种情况,我该如何解决?
【问题讨论】:
标签: php curl wget file-get-contents
我在 MacOS 上通过终端使用 PHP 命令 file_get_contents() 以及 wget 和 curl 时遇到了问题。
问题在于,对于某些网站,我只能得到一个 HTML 文件或奇怪符号的文本。我猜它必须与编码有关,但我没有找到任何东西。
为什么会发生这种情况,我该如何解决?
【问题讨论】:
标签: php curl wget file-get-contents
检测它是哪种编码。试试下面这行。
echo mb_detect_encoding($str); //$str is what you get after using file_get_contents
它不依赖于操作系统,而是特定于内容。
【讨论】:
在您的请求标头中包含此字段是个好主意:
Accept */*
Accept-Encoding gzip, deflate
【讨论】: