【问题标题】:CURL, wget and file_get_contentsCURL、wget 和 file_get_contents
【发布时间】:2012-10-26 13:30:10
【问题描述】:

我在 MacOS 上通过终端使用 PHP 命令 file_get_contents() 以及 wgetcurl 时遇到了问题。

问题在于,对于某些网站,我只能得到一个 HTML 文件或奇怪符号的文本。我猜它必须与编码有关,但我没有找到任何东西。

为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: php curl wget file-get-contents


    【解决方案1】:

    检测它是哪种编码。试试下面这行。

    echo mb_detect_encoding($str); //$str is what you get after using file_get_contents
    

    它不依赖于操作系统,而是特定于内容。

    【讨论】:

    • 同意,编码取决于内容。
    【解决方案2】:

    在您的请求标头中包含此字段是个好主意:

    Accept  */*
    Accept-Encoding gzip, deflate
    

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多