且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发送的字节数组从PHP到WCF

更新时间:2023-11-09 12:17:34

您应该使用字符串在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.