2010-05-26 11 views
6

मैं PHP और SOAP का उपयोग कर क्लाइंट और सर्वर के बीच फ़ाइलों (.zip फ़ाइलों) को स्थानांतरित करने का तरीका सीखने की कोशिश कर रहा हूं। वर्तमान में मैं एक स्थापित किया है ऊपर कुछ इस तरह दिखता है कि:PHP SOAP फ़ाइलों को स्थानांतरित करना

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

लेकिन मैं क्या वापसी प्रकार होना चाहिए नहीं तो एक स्ट्रिंग पर अनिश्चित हूं? मैं base64_encode का उपयोग करने के बारे में सोच रहा हूं।

उत्तर

1

एसओएपी पर फ़ाइलों को स्थानांतरित करना कुछ ऐसा है जो हर किसी को पहली बार (स्वयं शामिल) प्राप्त करता है। आपको दस्तावेज़ खोलने और पढ़ने की आवश्यकता है और फिर इसे एक स्ट्रिंग के रूप में स्थानांतरित करने की आवश्यकता है। यहां बताया गया है कि मैं इसे कैसे करूंगा।

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

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

फिर $ सोप के माध्यम से अपने स्ट्रिंग के रूप में सामग्री भेजने के लिए और दूसरी तरफ यह पुनः।

+0

जब आप कहते हैं कि reassamble, आप एक फाइल करने के लिए इसे बाहर लिखना क्या मतलब है? – user293313

+0

इसलिए मैंने कोशिश की कि एक फ़ाइल और सर्वर की ओर, फ़ाइल मौजूद होगी, लेकिन जब मैं इसे क्लाइंट के माध्यम से वापस स्थानांतरित करता हूं, तो यह एक खाली चर के रूप में आ रहा है। – user293313

+0

हां, इसे एक नई फाइल में लिखें। यदि यह पैकेट भेजने के बाद खाली के रूप में आ रहा है तो आपको शायद अपने वेब सेवा कोड में एक बग मिला है। –

12

अधिक स्पष्ट होने के लिए मैंने server.php कोड और client.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); 

============================ ==========================================12124======

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

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

आपको बस इतना करना है कि आप nusoap डाउनलोड करें। php जो साबुन लाइब्रेरी में देखा जाएगा http://sourceforge.net/projects/nusoap/

यह पूरी तरह से परीक्षण किया गया है और यह असफल होने पर 100% काम करेगा।

+0

मैं बेस 64 एन्कोडिंग पास करके छवि (आपके उपरोक्त कोड का उपयोग करके) अपलोड करने की कोशिश कर रहा हूं लेकिन निम्न त्रुटि: 'चेतावनी: file_put_contents() [function.file-put-content]: केवल 0 9 061 बाइट लिखे गए हैं, संभवतः बाहर पंक्ति 233 पर /home/example/public_html/example/example/server.php में मुफ्त डिस्क स्थान का –

1

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 
इस के लिए

:

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 
} 
संबंधित मुद्दे