将字节数组从 PHP 发布到 .NET WCF 服务 [英] Post byte array from PHP to .NET WCF Service

查看:33
本文介绍了将字节数组从 PHP 发布到 .NET WCF 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got a WCF service with a method to receive files, looking something like this

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

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

What I'd like to do is to post the data to this method in the WCF service from PHP, but are unaware if it's even possible to post byte arrays from PHP to a .NET method hosted by a WCF service.

所以我在想这样的事情

$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);

这是否可行,或者是否有任何我应该了解的一般性建议?

Would this be possible or are there any general recommendations out there I should know about?

推荐答案

想通了.php 官方文档告诉 file_get_contents 将整个文件作为字符串返回(http://php.net/manual/en/function.file-get-contents.php).没有人告诉我们这个字符串在发布到 WCF 服务时与 .NET bytearray 兼容.

Figured it out. The official php documentation tells that the file_get_contents returns the entire file as a string (http://php.net/manual/en/function.file-get-contents.php). What noone tells is that this string is compatible with the .NET bytearray when posted to a WCF service.

见下面的例子.

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

干杯!

这篇关于将字节数组从 PHP 发布到 .NET WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆