問題描述
我必須從我的 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!