PHP SOAP 传输文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2913106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:06:23  来源:igfitidea点击:

PHP SOAP Transferring Files

phpsoapsoap-client

提问by user293313

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:

我正在尝试学习如何使用 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');

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

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

回答by Sayyed Abdul Subhan

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

为了更清楚,我已经发布了 server.php 代码和 client.php 代码。请参阅以下内容:

## server.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 ##

## 客户端.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/

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

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

这是经过全面测试的,它将 100% 正常工作。

回答by Fabián

In client.php, change this:

在 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

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
}

回答by Jarrod Nettles

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.

通过 SOAP 传输文件是每个人第一次(包括我自己)的事情。您需要打开并阅读文档,然后将其作为字符串传输。这是我将如何做到的。

$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.

然后通过 SOAP 将 $contents 作为您的字符串发送并在另一侧重新组合。