2010-05-26 20 views
6

Próbuję dowiedzieć się, jak przesyłać pliki (pliki .zip) między klientem a serwerem za pomocą PHP i SOAP. Obecnie mam skonfigurować, który wygląda mniej więcej tak:PHP SOAP Transfer plików

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

Ale jestem pewien, na co typ zwracany powinien być jeśli nie ciąg? Myślę o używaniu base64_encode.

Odpowiedz

1

Przesyłanie plików przez SOAP to coś, co dostaje wszystkich za pierwszym razem (łącznie z mną). Musisz otworzyć i przeczytać dokument, a następnie przesłać go jako ciąg znaków. Oto, jak bym to zrobił.

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

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

Następnie wyślij $ treść jako ciąg przez SOAP i ponownie zmontuj go po drugiej stronie.

+0

Kiedy mówisz reassamble, masz na myśli zapisz go do pliku? – user293313

+0

Próbowałem więc, że będzie plik i po stronie serwera, plik istnieje, ale kiedy przeniosę go z powrotem przez klienta, nadchodzi jako pusta zmienna. – user293313

+0

Tak, zapisz go do nowego pliku. Jeśli po wysłaniu pakietu pojawi się pusty, prawdopodobnie masz błąd w kodzie serwisowym. –

12

Aby być bardziej zrozumiałym, opublikowałem zarówno kod server.php, jak i client.php. Proszę zobaczyć poniżej:

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

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

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

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

Wszystko, co musisz zrobić, to pobrać nusoap. php, który będzie widoczny w bibliotece mydła http://sourceforge.net/projects/nusoap/

To jest w pełni przetestowane i będzie w 100% działać bezbłędnie.

+0

Próbuję przesłać obraz (używając powyższego kodu), przekazując kodowanie base64, ale z następującym błędem: 'Ostrzeżenie: file_put_contents() [function-file-put-contents]: Tylko 0 z 39061 bajtów zapisanych, prawdopodobnie poza wolnego miejsca na dysku w /home/example/public_html/example/example/server.php na linii 233' –

1

W client.php, zmień to:

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 

do tego:

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 
}