Post字节数组从PHP到.net WCF服务


Post byte array from PHP to .NET WCF Service

我得到了一个带有接收文件方法的WCF服务,看起来像这样

public bool UploadFile(string fileName, byte[] data)
{
   //...
}

我想做的是将数据从PHP发布到WCF服务中的此方法,但不知道是否有可能将字节数组从PHP发布到由WCF服务托管的。net方法。

所以我在想这样的东西

$file = file_get_contents($_FILES['Filedata']['tmp_name']); // get the file content
$client = new SoapClient('http://localhost:8000/service?wsdl');
$params = array(
    'fileName' => 'whatever',
    'data' => $file 
);
$client->UploadFile($params);

这可能吗?或者有什么我应该知道的一般建议吗?

明白了。官方php文档告诉我们file_get_contents将整个文件作为字符串返回(http://php.net/manual/en/function.file-get-contents.php)。没有人告诉我们,当这个字符串被发送到WCF服务时,它与。net字节数组是兼容的。

见下面的例子

$filename = $_FILES["file"]["name"];
$byteArr = file_get_contents($_FILES['file']['tmp_name']);
try {
    $wsdloptions = array(
        'soap_version' => constant('WSDL_SOAP_VERSION'),
        'exceptions' => constant('WSDL_EXCEPTIONS'),
        'trace' => constant('WSDL_TRACE')
    );
    $client = new SoapClient(constant('DEFAULT_WSDL'), $wsdloptions);
    $args = array(
        'file' => $filename,
        'data' => $byteArr
    );

    $uploadFile = $client->UploadFile($args)->UploadFileResult;
    if($uploadFile == 1)
    {
        echo "<h3>Success!</h3>";
        echo "<p>SharePoint received your file!</p>";
    } 
    else
    {
        echo "<h3>Darn!</h3>";
        echo "<p>SharePoint could not receive your file.</p>";
    }

} catch (Exception $exc) {
    echo "<h3>Oh darn, something failed!</h3>";
    echo "<p>$exc->getTraceAsString()</p>";
}

干杯!