简体   繁体   English

Delphi,MSXML2.XMLHTTP,PHP和Win-1250字符集编码

[英]Delphi, MSXML2.XMLHTTP, PHP and Win-1250 charset encoding

Hoi! i!

I trying to make a webservice in Windows. 我试图在Windows中制作Web服务。

The client is Delphi 6, with MSXML2.XMLHTTP call, and other side is PHP. 客户端是Delphi 6,具有MSXML2.XMLHTTP调用,而另一端是PHP。

First I tested: can I receive hungarian XML? 首先,我进行了测试:我可以接收匈牙利XML吗?

The PHP source was UTF-8 encoded file (PSPAD). PHP源是UTF-8编码文件(PSPAD)。

$s = 'alma árvíztűrő tükörfúrógép beta';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$m = $doc->createElement('package');
$doc->appendChild($m);
$n = $doc->createElement('Msg');
$m->appendChild($n);
$n->nodeValue = $s;
$xs = $doc->saveXML();
header('Content-Type: text/xml');
echo($xs);

This package I fully got in Delphi side, the accents are ok. 我完全在Delphi方面得到的这个包装,口音还可以。

So then I tried to inject data from xml (post xml to php with accents). 因此,然后我尝试从xml注入数据(使用重音符号将xml发布到php)。

global $HTTP_RAW_POST_DATA;
$xmlstr = $HTTP_RAW_POST_DATA;
$xml = new SimpleXMLElement($xmlstr);
$msg = $xml->msg;
#$msg = 'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP';

Ok, I got the "msg" field, but nevertheless I trying to convert it, everytime I got some convert notice, and the result is not same as when I set the variable directly... 好的,我得到了“ msg”字段,但是每次我得到一些转换通知时,我都试图对其进行转换,结果与直接设置变量时的结果不同...

The debug is: 调试是:

echo(utfCharToNumber($sinput).'<br>');
echo(utfCharToNumber($sdefined).'<br>');

Input:   195[   ]8286195141908419717682197[   ]328419
Defined: 195[129]8286195141908419717682197[144]328419
Input:   5156751951508270195154821951477119513780<br>
Defined: 5156751951508270195154821951477119513780<br>

As you see that two holes I have in the variable when I converted the input from MSXML2. 如您所见,当我转换MSXML2的输入时,变量中有两个孔。

I really don't understand this. 我真的不明白。

I cannot reproduce same XML output from get the data from input XML as when I set directly in PHP code... 我无法直接从PHP代码中进行设置,而无法从输入XML中获取数据来重现XML输出...

Why? 为什么?

Thanks for your every help, idea, link, document, little example! 感谢您的每一个帮助,想法,链接,文档,小例子!

dd dd

由于您尚未包括Delphi源代码,因此我怀疑您是直接从字符串中发布数据作为请求的内容主体,默认情况下在Delphi 6中Utf8Encode其编码为当前ANSI编码。我建议您对字符串使用Utf8Encode在将其添加为请求的正文数据之前,或添加带有ANSI编码名称的“ Content-encoding”请求标头(如果我没记错的话, GetLocaleInfo可以为您提供)。

The source of the problem was the Delphi code. 问题的根源是Delphi代码。

Priorly I used AnsiToUTF8 to encode the XML text. 以前,我使用AnsiToUTF8编码XML文本。 But the COM object is uses UTF16 as I think. 但是我认为COM对象使用UTF16。

The working code is this: 工作代码是这样的:

procedure TForm1.Button4Click(Sender: TObject);
var
    mhttp : variant;
    ws : WideString;
    tosend : OleVariant;
    xml : TXMLDocument;
    n : IXMLNode;
begin
    mhttp := CreateOleObject('MSXML2.XMLHTTP');
    mhttp.Open('POST', 'http://127.0.0.1/test_xmlgen.php', False);

    xml := CreateANewDocument(Self, '', 'a');
    n := xml.DocumentElement.AddChild('msg');
    n.NodeValue := 'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP';
    xml.SaveToXML(ws);
    tosend := ws;

    mhttp.send(tosend);
    Memo1.Lines.Text :=
        IntToStr(mhttp.Status) + #13 +
        mhttp.responseText + #13;
end;

This can resend the XML I sent - with good accents. 这样可以重新发送我发送的XML-带有很好的重音。

Thanks for your help: dd 感谢您的帮助:dd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM