响应XML包含"2000"和"20a0&"人物 [英] Response XML contains " 2000 " and "20a0" characters

查看:44
本文介绍了响应XML包含"2000"和"20a0&"人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PHP发送的WebDAV属性请求.HTTP请求如下所示:

I have a WebDAV propfind request sent using PHP. The HTTP request looks like this:

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 之类的字符.

It works fine when the response XML is less than about 1.5 MB. When the response is bigger, the XML contains characters like \r\n2000\r\n and occasionaly \r\n20a0\r\n.

我正在使用以下PHP代码来检索响应:

I am using this PHP code to retrieve the response:

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

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

I can get around this issue by stripping the unwanted characters from the response - but I'd like to prevent this. Any idea what could cause this?

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

Update: The response header contains Transfer-Encoding: chunked. Our PHP build is Windows and I believe there is no DLL available to use http_chunked_decode().

推荐答案

正如一些人已经在注释中指出的那样,由于响应为

As several people already have pointed out in the comments the "hex"-characters are inserted because of the response being chunked-encoded.

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

This stack-overflow question deals with the same issue (not using the PECL extension) and suggests the following code-snippet for decoding the response:

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 .

As pointed out in the linked question, make sure the header Transfer-Encoding: chunked is set before applying the decoding.

更新: Zend框架具有响应类,它也支持分块解码.请注意,Zend \ Http类可以用作独立组件(无需在您的应用程序中包含完整的框架!).

Update: The Zend-Framework features a Response class that also supports chunked decoding. Note that that Zend\Http classes can be used as a stand-alone components (no need to have the full framework included in your app!).

这篇关于响应XML包含"2000&quot;和"20a0&"人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆