将字节数组从 PHP 发送到 WCF

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

问题描述

我必须从我的 PHP 客户端向 WCF 主机发送一个字节数组(编码照片).当我在 PHP 中对我的数组执行 var_dump() 时,我得到一个数组 [2839],这是可以的,但是在服务器端,当我调试时,我看到收到的数组只是字节 [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?

我用过这样的代码

$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);

正如我之前在 wcf 主机上写的那样,我只得到字节 [5] :/也许它需要一些解码才能正确地序列化肥皂?我应该使用 Base64 解码还是什么?

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?

一般我只想以字节[]为参数将发布的文件上传到c#函数:/帮助

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

哦,这个函数的 wsdl 部分看起来像这样

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>

推荐答案

您应该在 PHP 中使用字符串来模拟字节数组.您甚至可以将语法 $str[index] 与字符串一起使用.否则,您将有巨大的开销(4 倍或 8 倍,具体取决于有效负载的整数大小加上哈希表开销).

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.

我不太熟悉 SOAP 扩展所做的类型转换,但使用字符串可能会起作用.

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

刚刚检查了来源:

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);
}

所以它已经为你做了 base 64 编码.

So it already does the base 64 encoding for you.

[推测]

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

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);
}

转换结果为数组",长度为 5 个字节.

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

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

上一篇:PHP 中的 SoapClient 错误回退 下一篇:PHP中P_SHA1算法的实现

相关文章

最新文章