您的位置:首页 > 编程语言 > PHP开发

PHP SOAP client pass array to C# SOAP webservice(PHP SOAP 数组参数传递)

2013-08-15 08:31 435 查看
        由于前面同事是用C#实现soap webservice的,有一个函数中用到了数组变量byte [] buffer,这个用C#的soap client是可以毫无压力的实现的,且功能正常。

        但是现版本中client需用php来实现,在这个数组变量传递部分卡住了,传递过去的感觉是数组的地址,而非数组内容。

  以下是错误代码!!!!

$arybuff =array(1,2,3,4,5,6);

$aryPara = array('site'=>5,'cmd'=>2,'buffer'=>$arybuff,'IP'=>'192.168.1.64');

$result1 = $soap->YSSocketSend20($aryPara);
        php会产生告警,提示将数组转换成了字符串。传递过去的buffer是错误的,并非1,2,3……更像是一个地址值。
       OK,这时候软件对SOAP进行抓包,以下信息眼前一亮。

<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="site" type="s:unsignedByte" />
<s:element minOccurs="1" maxOccurs="1" name="cmd" type="s:unsignedShort" />
<s:element minOccurs="0" maxOccurs="1" name="buffer" type="s:base64Binary" />
<s:element minOccurs="0" maxOccurs="1" name="IP" type="s:string" />
</s:sequence>
base64Binary,这个是buffer传递的数据类型,知道传递的是什么东西后,接下来的问题就是要将传递的数组转换成base64Binary类型的数据。
但是接下来的代码也是错误的!!!!!

$arybuff =array(1,2,3,4,5,6);
$text ="";
$i=0;
for ( $i=0; $i<count($arybuff);$i++) {
$text .= chr($arybuff[$i]);
}
$text = base64_encode($text);
$aryPara = array('site'=>5,'cmd'=>2,'buffer'=>$text,'IP'=>'192.168.1.64');

$result1 = $soap->YSSocketSend20($aryPara);

百思不得其解中看到了如下信息跟代码:http://php.net/manual/ru/soapclient.soapclient.php,原来SOAP库已经将数据base64了。

If your WSDL file containts a parameter with a base64Binary type, you should not
use base64_encode() when passing along your soap vars. When doing the request, the SOAP library automatically base64 encodes your data, so otherwise you'll be encoding it twice.

<?php
$string = 'data_you_want_to_send___like_xml_in_soap';
$soap_data = array(
'foo' => 'bar',
//'content' => base64_encode($string ) // don't do this
'content' => $string //do this
);
$response = $client->Send($soap_data);
?>


所以正确的代码应该是这样的:
$arybuff =array(1,2,3,4,5,6);
$text ="";
$i=0;
for ( $i=0; $i<count($arybuff);$i++) {
$text .= chr($arybuff[$i]);
}
//$text = base64_encode($text);
$aryPara = array('site'=>5,'cmd'=>2,'buffer'=>$text,'IP'=>'192.168.1.64');

$result1 = $soap->YSSocketSend20($aryPara);


       花了差不多一天时间才搞定这个问题,简单写了篇博客记录下解决问题的过程,还是得多看php官方的PHP Manual才行。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: