file_get_contents在下载二进制文件时附加数据

时间:2016-02-10 11:02:42

标签: php

我编写了一个允许我在PHP中发送HTTP GET请求的函数:

function get_HTTPS_page_with_version($url, $hostname, $use_HTTP_1_0 = false, &$headers = NULL, $follow_redirect = true, $use_SSL = true) {
  $context = Array(
    "http" => Array(
      "method" => "GET",
      "ignore_errors" => true,
      "follow_location" => $follow_redirect,
      "user_agent" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
      "header" => "Accept-language: en")
  );

  if ($use_SSL) {
    $context["ssl"] = Array(
      "peer_name" => $hostname,
      "SNI_server_name" => $hostname,
      "verify_peer_name" => false,
      "verify_peer" => false);
  }

  if (!$use_HTTP_1_0) {
    $context["http"]["protocol_version"] = "1.1";
    $context["http"]["header"] .= "\r\nHost: $hostname".
                                  "\r\nConnection: close";
  }

  $page = file_get_contents($url, false, stream_context_create($context));
  $responseCode = get_HTTP_response_code($http_response_header);
  $headers = $http_response_header;

  return Array($responseCode, $page);
}

问题在于,当我使用此函数获取特定文件(Verisign Certificate Revocation List)时,它会在开头附加一些字符:

6639 6361 0a0d --> f9 ca \n\r

并在文件的末尾:

0a0d 0d30 0d0a 000a --> \n\r \r0 \r\n NUL\n

我比较了使用Wget和使用此功能手动获取的文件,以及使用Wireshark在网络级别获得的文件,我可以确认服务器发送的文件在两种情况下都是相同的。

我也没有Thawte CRL

的问题

有没有人知道可能导致这种行为的原因?

修改

Verisign CRL已更改,其他字节由file_get_contents添加,而不是我上面列出的那些,但结果仍然相同 - >使用此功能下载文件时,文件与手动使用wget不同。

编辑2

  • 设置$use_HTTP_1_0 = true时没有问题。
  • 添加标题Accept-Encoding: gzip, deflate时没有问题(除了我得到一个gz文件,我宁愿避免 这个:))
  • 当我仅从Connection更改close时,仍然存在问题 到Keep-Alive

0 个答案:

没有答案
相关问题