PHP SOAP 传输文件

发布于 2024-09-03 12:16:05 字数 533 浏览 3 评论 0原文

我正在尝试学习如何使用 PHP 和 SOAP 在客户端和服务器之间传输文件(.zip 文件)。目前我的设置看起来像这样:

require('libraries/nusoap/nusoap.php');

$server = new nusoap_server;

$server->configureWSDL('server', 'urn:server');

$server->wsdl->schemaTargetNamespace = 'urn:server';

$server->register('sendFile',
            array('value' => 'xsd:string'),
            array('return' => 'xsd:string'),
            'urn:server',
            'urn:server#sendFile');

但我不确定如果不是字符串,返回类型应该是什么?我正在考虑使用 base64_encode

I am trying to learn how to transfer files (.zip files) between a client and server using PHP and SOAP. Currently I have a set up that looks something like this:

require('libraries/nusoap/nusoap.php');

$server = new nusoap_server;

$server->configureWSDL('server', 'urn:server');

$server->wsdl->schemaTargetNamespace = 'urn:server';

$server->register('sendFile',
            array('value' => 'xsd:string'),
            array('return' => 'xsd:string'),
            'urn:server',
            'urn:server#sendFile');

But I am unsure on what the return type should be if not a string? I am thinking of using a base64_encode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2024-09-10 12:16:05

为了更清楚起见,我发布了 server.php 代码和 client.php 代码。请参见下面:

## server.php ##

require_once('lib/nusoap.php'); //include required class for build nnusoap web service server

  // Create server object
   $server = new soap_server();

   // configure  WSDL
   $server->configureWSDL('Upload File', 'urn:uploadwsdl');

   // Register the method to expose
    $server->register('upload_file',                                 // method
        array('file' => 'xsd:string','location' => 'xsd:string'),    // input parameters
        array('return' => 'xsd:string'),                             // output parameters
        'urn:uploadwsdl',                                            // namespace
        'urn:uploadwsdl#upload_file',                                // soapaction
        'rpc',                                                       // style
        'encoded',                                                   // use
        'Uploads files to the server'                                // documentation
    );

    // Define the method as a PHP function

    function upload_file($encoded,$name) {
        $location = "uploads\\".$name;                               // Mention where to upload the file
        $current = file_get_contents($location);                     // Get the file content. This will create an empty file if the file does not exist     
        $current = base64_decode($encoded);                          // Now decode the content which was sent by the client     
        file_put_contents($location, $current);                      // Write the decoded content in the file mentioned at particular location      
        if($name!="")
        {
            return "File Uploaded successfully...";                      // Output success message                              
        }
        else        
        {
            return "Please upload a file...";
        }
    }

    // Use the request to (try to) invoke the service
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA); 

=========================================== ==============================

## client.php ##

require_once('lib/nusoap.php'); //include required class for build nnusoap web service server
   $wsdl="http://localhost:81/subhan/webservice3/server.php?wsdl";  // SOAP Server

   if($_POST['submit'])
   {
       $tmpfile = $_FILES["uploadfiles"]["tmp_name"];   // temp filename
       $filename = $_FILES["uploadfiles"]["name"];      // Original filename

       $handle = fopen($tmpfile, "r");                  // Open the temp file
       $contents = fread($handle, filesize($tmpfile));  // Read the temp file
       fclose($handle);                                 // Close the temp file

       $decodeContent   = base64_encode($contents);     // Decode the file content, so that we code send a binary string to SOAP
    }   

   $client=new soapclient($wsdl) or die("Error");   // Connect the SOAP server
   $response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error");  //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME

   // Check if there is anny fault with Client connecting to Server
   if($client->fault){
        echo "Fault {$client->faultcode} <br/>";
        echo "String {$client->faultstring} <br/>";
   }
   else{
        print_r($response); // If success then print response coming from SOAP Server
   }


<form name="name1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="uploadfiles"><br />
<input type="submit" name="submit" value="uploadSubmit"><br />
</form>

============= ======================================

您需要做的就是下载 nusoap.php,它将在肥皂库中看到 http://sourceforge.net/projects/nusoap/

这已经过充分测试,它将 100% 工作,无需失败。

To be more clear I have posted both the server.php code and client.php code. Please see below:

## server.php ##

require_once('lib/nusoap.php'); //include required class for build nnusoap web service server

  // Create server object
   $server = new soap_server();

   // configure  WSDL
   $server->configureWSDL('Upload File', 'urn:uploadwsdl');

   // Register the method to expose
    $server->register('upload_file',                                 // method
        array('file' => 'xsd:string','location' => 'xsd:string'),    // input parameters
        array('return' => 'xsd:string'),                             // output parameters
        'urn:uploadwsdl',                                            // namespace
        'urn:uploadwsdl#upload_file',                                // soapaction
        'rpc',                                                       // style
        'encoded',                                                   // use
        'Uploads files to the server'                                // documentation
    );

    // Define the method as a PHP function

    function upload_file($encoded,$name) {
        $location = "uploads\\".$name;                               // Mention where to upload the file
        $current = file_get_contents($location);                     // Get the file content. This will create an empty file if the file does not exist     
        $current = base64_decode($encoded);                          // Now decode the content which was sent by the client     
        file_put_contents($location, $current);                      // Write the decoded content in the file mentioned at particular location      
        if($name!="")
        {
            return "File Uploaded successfully...";                      // Output success message                              
        }
        else        
        {
            return "Please upload a file...";
        }
    }

    // Use the request to (try to) invoke the service
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA); 

=====================================================================

## client.php ##

require_once('lib/nusoap.php'); //include required class for build nnusoap web service server
   $wsdl="http://localhost:81/subhan/webservice3/server.php?wsdl";  // SOAP Server

   if($_POST['submit'])
   {
       $tmpfile = $_FILES["uploadfiles"]["tmp_name"];   // temp filename
       $filename = $_FILES["uploadfiles"]["name"];      // Original filename

       $handle = fopen($tmpfile, "r");                  // Open the temp file
       $contents = fread($handle, filesize($tmpfile));  // Read the temp file
       fclose($handle);                                 // Close the temp file

       $decodeContent   = base64_encode($contents);     // Decode the file content, so that we code send a binary string to SOAP
    }   

   $client=new soapclient($wsdl) or die("Error");   // Connect the SOAP server
   $response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error");  //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME

   // Check if there is anny fault with Client connecting to Server
   if($client->fault){
        echo "Fault {$client->faultcode} <br/>";
        echo "String {$client->faultstring} <br/>";
   }
   else{
        print_r($response); // If success then print response coming from SOAP Server
   }


<form name="name1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="uploadfiles"><br />
<input type="submit" name="submit" value="uploadSubmit"><br />
</form>

=================================================

All you need to do is download the nusoap.php which will be seen in soap library http://sourceforge.net/projects/nusoap/

This is fully tested and it will 100% work without fail.

半衬遮猫 2024-09-10 12:16:05

在 client.php 中,将以下内容更改

if($_POST['submit'])
{

   ...

}   
$client=new soapclient($wsdl) or die("Error");   // Connect the SOAP server
$response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error");  //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME

为:

if($_POST['submit'])
{
   ...

   $client=new soapclient($wsdl) or die("Error");   // Connect the SOAP server
   $response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error");  //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME
}

In client.php, change this:

if($_POST['submit'])
{

   ...

}   
$client=new soapclient($wsdl) or die("Error");   // Connect the SOAP server
$response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error");  //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME

to this:

if($_POST['submit'])
{
   ...

   $client=new soapclient($wsdl) or die("Error");   // Connect the SOAP server
   $response = $client->__call('upload_file',array($decodeContent,$filename)) or die("Error");  //Send two inputs strings. {1} DECODED CONTENT {2} FILENAME
}
一身仙ぐ女味 2024-09-10 12:16:05

通过 SOAP 传输文件对于每个人(包括我自己)来说都是第一次。您需要打开并读取文档,然后将其作为字符串传输。我就是这样做的。

$handle = fopen("mypackage.zip", "r");
$contents = fread($handle, filesize("mypackage.zip"));
fclose($handle);

//$contents now holds the byte-array of our selected file

然后通过 SOAP 将 $contents 作为字符串发送,并在另一端重新组装它。

Transferring files over SOAP is something that gets everybody the first time (myself included). You need to open and read the document and then transfer it as a string. Here's how I would do it.

$handle = fopen("mypackage.zip", "r");
$contents = fread($handle, filesize("mypackage.zip"));
fclose($handle);

//$contents now holds the byte-array of our selected file

Then send $contents as your string through SOAP and reassemble it on the other side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文