2010-05-26 33 views
6

Tôi đang cố gắng tìm hiểu cách chuyển tệp (tệp .zip) giữa máy khách và máy chủ bằng cách sử dụng PHP và SOAP. Hiện nay tôi có một thiết lập mà trông giống như sau:PHP SOAP Truyền tệp

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

Nhưng tôi không chắc chắn về những gì các kiểu trả về nên nếu không phải là một chuỗi? Tôi đang nghĩ đến việc sử dụng một số base64_encode.

Trả lời

1

Truyền tệp qua SOAP là một thứ được mọi người lần đầu tiên (bao gồm cả tôi). Bạn cần phải mở và đọc tài liệu và sau đó chuyển nó thành một chuỗi. Đây là cách tôi sẽ làm điều đó.

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

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

Sau đó gửi nội dung $ làm chuỗi của bạn thông qua SOAP và lắp ráp lại ở phía bên kia.

+0

Khi bạn nói reassamble, làm bạn có nghĩa là viết nó ra vào một tập tin? – user293313

+0

Vì vậy, tôi đã thử rằng sẽ một tập tin và ở phía máy chủ, các tập tin tồn tại, nhưng khi tôi chuyển nó trở lại thông qua khách hàng, nó là sắp tới như là một biến trống. – user293313

+0

Có, hãy viết nó ra một tệp mới. Nếu nó sắp lên như một sản phẩm nào sau khi gửi gói thì có thể bạn đã gặp lỗi trong mã dịch vụ web của mình. –

12

Để rõ ràng hơn, tôi đã đăng cả mã server.php và mã client.php. Xin vui lòng xem bên dưới:

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

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

Tất cả những gì bạn cần làm là tải xuống nusoap. php sẽ được nhìn thấy trong thư viện xà phòng http://sourceforge.net/projects/nusoap/

Điều này được kiểm tra đầy đủ và nó sẽ hoạt động 100% mà không bị lỗi.

+0

Tôi đang cố gắng tải lên hình ảnh (sử dụng mã trên của bạn) bằng cách chuyển mã base64 nhưng có lỗi sau: 'Cảnh báo: file_put_contents() [function.file-put-contents]: Chỉ 0 trong 39061 byte được viết, có thể là dung lượng đĩa trống trong /home/example/public_html/example/example/server.php trên dòng 233' –

1

Trong client.php, thay đổi này:

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 

này:

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 
}