从PHP到.NET WCF服务发布字节数组

时间:2011-08-09 08:20:43

标签: c# php wcf

我获得了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);

这是可能的,还是有任何我应该知道的一般性建议?

1 个答案:

答案 0 :(得分:5)

想出来。 官方php文档告诉file_get_contents将整个文件作为字符串返回(http://php.net/manual/en/function.file-get-contents.php)。没有人告诉的是,当发布到WCF服务时,此字符串与.NET bytearray兼容。

见下面的例子。

$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>";
}

干杯!