发送的字节数组从PHP到WCF [英] Sending a byte array from PHP to WCF

查看:184
本文介绍了发送的字节数组从PHP到WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从我的PHP客户端发送一个字节数组(编码照片)到WCF主机。当我在PHP做的var_dump()我阵列我得到一个数组[2839],这是好的,但是,当我调试我看到收到阵列只有字节[5] ...任何想法如何,我可以解决服务器端呢?



我用这样的

  $文件=的file_get_contents代码($ _FILES ['Filedata上'] ['tmp_name的值']); 
$ byteArr = str_split($文件);
的foreach($ byteArr为$关键=> $ VAL){$ byteArr [$关键] = ORD($ VAL); }

$ =客户端和SoapClient新(的http://本地主机:8000 / MgrService WSDL',
阵列(
'位置'=>的http://本地主机:8000 / MgrService / SOAP11',
'跟踪'=>真,
'soap_version'=> SOAP_1_1
));
$ par1->配置文件ID = 13;
$ par1->照片= $ byteArr;

$客户 - > TestByte($ PAR1);

和我的WCF主机上之前写我得到的[5]只有字节:/也许它需要一些解码向右肥皂序列化? ?我应该使用Base64编码解码什么



一般我只是想上传发布文件到C#功能的字节[]作为参数:/帮助



呵呵,这功能的WSDL部分看起来像这样

 < XS:元素的名称= TestByte> 
< XS:复杂类型>
< XS:序列>
< XS:元素的minOccurs =0NA​​ME =照片的nillable =真TYPE =的xs:base64Binary的/>
< / XS:序列>
< / XS:复杂类型>
< / XS:组件>


解决方案

您应该使用字符串在PHP中模仿字节数组。你甚至可以使用语法 $海峡[指数] 处理字符串。你必须(根据INT尺寸有效载荷PLUS哈希表开销4倍倍或8倍),否则一个巨大的开销。



我不是很熟悉的类型转换。SOAP扩展的做法,但使用的是字符串,而不是可能会工作。



编辑:只是检查来源:

 如果(Z_TYPE_P(数据)== IS_STRING){
海峡= php_base64_encode((无符号字符*)Z_STRVAL_P(数据),Z_STRLEN_P(数据),放大器; str_len);
文本= xmlNewTextLen(STR,str_len);
xmlAddChild(RET,文字);
efree(STR);
}



因此,它已经做了base 64编码为您服务。



EDIT2:[思索]



您5字节长的结果,是因为遵循上面的代码转换为字符串:

 如果(Z_TYPE_P(数据)== IS_STRING){

}其他{
的zval TMP = *的数据;

zval_copy_ctor(安培; TMP);
convert_to_string(安培; TMP);
海峡= php_base64_encode((无符号字符*)Z_STRVAL(TMP),Z_STRLEN(TMP),放大器; str_len);
文本= xmlNewTextLen(STR,str_len);
xmlAddChild(RET,文字);
efree(STR);
zval_dtor(安培; TMP);
}

在阵,这是5个字节长的转换结果。


I have to send a byte array (encoded photo) from my PHP client to the WCF host. when I do a var_dump() on my array in PHP I get an array[2839] which is ok but on the server side when I debug I see that received array is only byte[5]... Any idea how I can fix it?

I used code like this

$file = file_get_contents($_FILES['Filedata']['tmp_name']);
        $byteArr = str_split($file);
        foreach ($byteArr as $key=>$val) { $byteArr[$key] = ord($val); }

$client = new SoapClient('http://localhost:8000/MgrService?wsdl',
                    array(
                    'location' => 'http://localhost:8000/MgrService/SOAP11',
                    'trace' => true,
                    'soap_version' => SOAP_1_1
                    ));
  $par1->profileId = 13;
  $par1->photo = $byteArr;          

  $client->TestByte($par1);

And as I wrote earlier on the wcf host I get only byte[5] :/ maybe it needs some decoding to right soap serialize? should I use Base64 decoding or something?

General I just want to upload posted file to c# function with byte[] as parameter :/ Help

Oh and the wsdl part of this function looks like this

<xs:element name="TestByte">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="photo" nillable="true" type="xs:base64Binary"/>
</xs:sequence>
</xs:complexType>
</xs:element>

解决方案

You should use strings in PHP to emulate byte arrays. You can even use the syntax $str[index] with strings. You have a HUGE overhead (4x or 8x depending on the int size the payload PLUS the hash table overhead) otherwise.

I'm not very familiar with the type conversions the SOAP extension does, but using a string instead will probably work.

EDIT: Just checked the sources:

if (Z_TYPE_P(data) == IS_STRING) {
    str = php_base64_encode((unsigned char*)Z_STRVAL_P(data), Z_STRLEN_P(data), &str_len);
    text = xmlNewTextLen(str, str_len);
    xmlAddChild(ret, text);
    efree(str);
}

So it already does the base 64 encoding for you.

EDIT2: [SPECULATION]

Your 5-byte long result is because of the conversion to string that follows the code above:

if (Z_TYPE_P(data) == IS_STRING) {
        ...
} else {
    zval tmp = *data;

    zval_copy_ctor(&tmp);
    convert_to_string(&tmp);
    str = php_base64_encode((unsigned char*)Z_STRVAL(tmp), Z_STRLEN(tmp), &str_len);
    text = xmlNewTextLen(str, str_len);
    xmlAddChild(ret, text);
    efree(str);
    zval_dtor(&tmp);
}

The conversion results in "Array", which is 5 bytes long.

这篇关于发送的字节数组从PHP到WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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