Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SOAP Transferring Files

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.

like image 476
user293313 Avatar asked May 26 '10 12:05

user293313


People also ask

What is the use of SOAP in PHP?

The soap:encodingStyle attribute determines the data types used in the file, but SOAP itself does not have a default encoding. soap:Envelope is mandatory, but the next element, soap:Header , is optional and usually contains information relevant to authentication and session handling.

What is SoapClient in php?

Description ¶ This is a low level API function that is used to make a SOAP call. Usually, in WSDL mode, SOAP functions can be called as methods of the SoapClient object. This method is useful in non-WSDL mode when soapaction is unknown, uri differs from the default or when sending and/or receiving SOAP Headers.

What is a SoapClient?

A SOAP client formulates a request for a service. This involves creating a conforming XML document, either explicitly or using Oracle SOAP client API. A SOAP client sends the XML document to a SOAP server. This SOAP request is posted using HTTP or HTTPS to a SOAP Request Handler running as a servlet on a Web server.

What is NuSOAP?

NuSOAP is a SOAP Toolkit for PHP that doesn't require PHP extensions.


2 Answers

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.

like image 133
Sayyed Abdul Subhan Avatar answered Nov 15 '22 22:11

Sayyed Abdul Subhan


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
}
like image 22
Fabián Avatar answered Nov 15 '22 22:11

Fabián