Sending a byte array from PHP to WCF

◇◆丶佛笑我妖孽 提交于 2019-11-29 23:59:04

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/2986151/sending-a-byte-array-from-php-to-wcf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!