2010-05-26 13 views
6

Sto cercando di imparare come trasferire file (file .zip) tra un client e un server utilizzando PHP e SOAP. Attualmente ho un set up che sembra qualcosa di simile:File di trasferimento SOAP PHP

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

Ma io sono sicuro su quello che il tipo di ritorno dovrebbe essere se non una stringa? Sto pensando di usare un base64_encode.

risposta

1

Il trasferimento di file su SOAP è qualcosa che ottiene tutti la prima volta (me compreso). È necessario aprire e leggere il documento e quindi trasferirlo come stringa. Ecco come lo farei.

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

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

Quindi inviare $ contenuto come stringa da SOAP e rimontarlo sull'altro lato.

+0

Quando si dice reassamble, vuoi dire scriverlo in un file? – user293313

+0

Così ho provato che sarà un file e sul lato server, il file esiste, ma quando lo ritrasfero attraverso il client, sta venendo fuori come una variabile vuota. – user293313

+0

Sì, scrivilo in un nuovo file. Se viene visualizzato come vuoto dopo l'invio del pacchetto, probabilmente hai un bug nel codice del tuo servizio web. –

12

Per essere più chiari ho postato sia il codice server.php che il codice client.php. Si prega di vedere di seguito:

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

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

Tutto ciò che devi fare è scaricare nusoap. php che sarà visibile nella soap library http://sourceforge.net/projects/nusoap/

Questo è completamente testato e funzionerà al 100% senza fallo.

+0

Sto tentando di caricare l'immagine (utilizzando il codice precedente) passando la codifica base64 ma con il seguente errore: 'Avviso: file_put_contents() [function.file-put-contents]: solo 0 di 39061 byte scritti, probabilmente fuori di spazio libero su disco in /home/example/public_html/example/example/server.php alla riga 233' –

1

In client.php, cambiare questo:

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 

a questo:

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 
}