响应XML包含“;2000〃;以及“;20a0”;字符


Response XML contains " 2000 " and "20a0" characters

我有一个使用PHP发送的WebDAV propfind请求。HTTP请求如下所示:

PROPFIND /path/to/whatever HTTP/1.1
User-Agent: My Client
Accept-Encoding: deflate
Depth: 1
Host: example.com
Content-Type: text/xml;charset=UTF-8
Authorization: Basic bLahDeBlah=
Content-Length: 82
Connection: close
<?xml version='1.0' encoding='utf-8'?><propfind xmlns='DAV:'><allprop/></propfind>

当响应XML小于1.5 MB时,它可以正常工作。当响应较大时,XML会包含类似'r'n2000'r'n和偶尔包含'r'n20a0'r'n的字符。

我使用这个PHP代码来检索响应:

<?php
$output = "";
while (!feof($this->socket)) {
        $output .= fgets($this->socket, 1024);
}

我可以通过从响应中删除不需要的字符来解决这个问题,但我想防止这种情况发生。知道是什么原因造成的吗?

更新:响应标头包含Transfer-Encoding: chunked。我们的PHP版本是Windows,我相信没有可用的DLL来使用http_chunked_decode()

正如一些人已经在评论中指出的那样,插入"十六进制"字符是因为对响应进行了分块编码。

这个堆栈溢出问题处理相同的问题(不使用PECL扩展),并建议使用以下代码片段来解码响应:

function decode_chunked($str) {
  for ($res = ''; !empty($str); $str = trim($str)) {
    $pos = strpos($str, "'r'n");
    $len = hexdec(substr($str, 0, $pos));
    $res.= substr($str, $pos + 2, $len);
    $str = substr($str, $pos + 2 + $len);
  }
  return $res;
}

如链接问题中所指出的,在应用解码之前,请确保设置了标头Transfer-Encoding: chunked

更新:Zend Framework具有一个响应类,该类也支持分块解码。请注意,Zend''Http类可以用作独立组件(不需要在应用程序中包含完整的框架!)。