Sending a File to a server with NuSoap

Help
2013-12-16
2013-12-16
  • Hi,
    I've maked a client that send some strings to a soap server and it works fine. Now I've to send a file too. This is the related wsdl part:

    <wsdl:part name="email" type="xsd:string"/>
    <wsdl:part name="p0" type="xsd:string"/>
    <wsdl:part name="dh" type="apachesoap:DataHandler"/>
    

    My client is client.php:

    <?php
    /*
    ...
     SOME STUFF
    ...
    */
    
    require 'assets/lib/nusoap.php';
    
    $email = $_POST['email'];
    $tmpZip = 'tmp/uploaded_files.zip';
    $p0 = $_POST['p0'];
    
    //instantiate the NuSOAP class and define the web service URL:
    $client = new nusoap_client('http://xyz.mydomain.com:8080/web10/services/web10?wsdl');
    $client->soap_defencoding = 'UTF-8';
    $client->decode_utf8 = false;
    
    //check if there were any instantiation errors, and if so stop execution with an error message:
    $error = $client->getError();
    
    if ($error) {
        die("client construction error: {$error} \n");
    }
    
    $user = 'frank';
    $psw = 'frank2013';
    
    //authenticate to the service:
    $client->setCredentials($user, $psw);
    
    //file part
    $tmpfile = BASE_PATH.'/'.$tmpZip;
    $handle = fopen($tmpfile, "r");                  // Open the temp file
    $contents = fread($handle, filesize($tmpfile));  // Read the temp file
    fclose($handle);
    
    //perform a function call with parameters:
    $param = array(
        'email' => $email,
        'p0' => $p0,
        'dh' => $contents
    );
    
    $answer = $client->call('newContact', $param, '', '', '', true, 'rpc', '');
    
    //check if there were any call errors, and if so stop execution with some error messages:
    if ($client->fault) {
        echo '<h2>Fault</h2><pre>';
        print_r($answer);
        echo '</pre>';
    } else {
        $err = $client->getError();
        if ($err) {
            echo  $err ;
        } else {
            //echo substr($answer, 46, 2);
            echo $answer;
        }
    }
    

    If I remove the file part, it send correctly the two strings. How can I send the file?
    Thank you very much

     
  • A working java Client is following:

    String serviceEndpoint =
    "http://xxx.mydomain.it:8080/web10/services/web10?wsdl";
    String method = "newContact";
    Service service = new Service();
    Call call = (Call) service.createCall();
    call.setUsername("frank");
    call.setPassword("frank2012");
    
    call.setTargetEndpointAddress(new java.net.URL(serviceEndpoint));
    call.setOperationName(method);
    call.setTimeout(-1);
    FileDataSource ds = new FileDataSource(new File("C:\\Users\\User\\Downloads\\auto2.zip"));
    DataHandler zipFileHandler = new DataHandler(ds);
    
    QName qnameAttachment = new QName(serviceEndpoint, "DataHandler");
    call.registerTypeMapping(zipFileHandler.getClass(),qnameAttachment,JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class);
    call.addParameter("email", XMLType.SOAP_STRING, ParameterMode.IN);
    call.addParameter("p0", XMLType.SOAP_STRING, ParameterMode.IN);
    call.addParameter("source", qnameAttachment, ParameterMode.IN);
    call.setReturnType(XMLType.SOAP_STRING);
    Object[] parametri = {"email","p0",zipFileHandler};
    
    String v = (String) call.invoke(parametri);
    System.out.println("Result newContact = "+v);
    
     
    Last edit: Serafino Bilotta 2013-12-16