douzhongjian0752 2012-06-11 13:38
浏览 74
已采纳

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

Hoi!

I trying to make a webservice in Windows.

The client is Delphi 6, with MSXML2.XMLHTTP call, and other side is PHP.

First I tested: can I receive hungarian XML?

The PHP source was UTF-8 encoded file (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.

So then I tried to inject data from xml (post xml to php with accents).

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...

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.

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...

Why?

Thanks for your every help, idea, link, document, little example!

dd

  • 写回答

2条回答 默认 最新

  • doukuai3822 2012-06-27 14:34
    关注

    The source of the problem was the Delphi code.

    Priorly I used AnsiToUTF8 to encode the XML text. But the COM object is uses UTF16 as I think.

    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.

    Thanks for your help: dd

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么