NuSOAP using Laravel 4

1

I'm creating a Webservice on Laravel with the Noiselabs library.

Route::any('x/ws/hello', function(){
    $server = new \soap_server;

    $server->configureWSDL('server.hello', 'urn:server.hello');
    $server->wsdl->schemaTargetNamespace = 'urn:server.hello';

    $server->register('hello',
            array('name' => 'xsd:string'),
            array('return' => 'xsd:string'),
            'urn:server.hello',
            'urn:server.hello#hello',
            'rpc',
            'encoded',
            'Retorna o nome'
    );

    function hello($name)
    {
        return 'Hello ' . $name;
    }

    // requisição para uso do serviço
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA);
});

Solongsogood.

ButwhenItrytoshowthegeneratedXMLwith?wsdlattheendoftheURLitgivesaproblem.GeneratesanHTMLfromthegeneratedXML.

I thought it might be the debug I'm using with Laravel, but I put it false in the app.php file and it also continued the same way.

When I do with pure PHP it worked.

My client looks like this.

Route::get('x/client/hello', function(){
    // criação de uma instância do cliente
    $client = new \nusoap_client('http://localhost/registro_aplicativos/public/x/ws/hello?wsdl', true);
    // verifica se ocorreu erro na criação do objeto

    $result = $client->call('hello', array('Renato Araujo'));
    // exibe o resultado
    var_dump($result);

    echo '<h2>Requisição</h2>';
    echo '<pre>' . htmlspecialchars($client->request) . '</pre>';
    echo '<h2>Resposta</h2>';
    echo '<pre>' . htmlspecialchars($client->response) . '</pre>';
    // Exibe mensagens para debug
    echo '<h2>Debug</h2>';
    echo '<pre>' . htmlspecialchars($client->debug_str) . '</pre>';
});
    
asked by anonymous 05.02.2014 / 15:09

2 answers

0

After some research I managed to solve.

I used Response :: make (); of Laravel.

Route::any('x/ws/hello', function(){
    $server = new \soap_server;

    $server->configureWSDL('server.hello', 'urn:server.hello');
    $server->wsdl->schemaTargetNamespace = 'urn:server.hello';

    $server->register('hello',
        array('name' => 'xsd:string'),
        array('return' => 'xsd:string'),
        'urn:server.hello',
        'urn:server.hello#hello',
        'rpc',
        'encoded',
        'Retorna o nome'
    );

    function hello($name)
    {
        return 'Hello ' . $name;
    }

    // requisição para uso do serviço
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

    return Response::make($server->service($HTTP_RAW_POST_DATA), 200, array('Content-Type' => 'text/xml', 'charset' => 'ISO-8859-1'));

});

    
06.02.2014 / 16:29
0

What is happening is that the browser is automatically "interpreting" the XML and presenting the result transformed into HTML.

The key to changing this behavior is to change the HTTP header that informs the "Content-Type" of the content sent to the browser.

Check what the Content-Type HTTP server is sending to the browser. (Use Firebug or another development tool.) I believe it is " text / xml ".

To change the Content-Type, there are several alternatives. In this case, we need a little help with output buffering ("output control"):

// requisição para uso do serviço
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

// iniciamos o output buffering...
ob_start();

// ...para podermos chamar a função do nuSOAP
$server->service($HTTP_RAW_POST_DATA);

// aqui sobrescrevemos o Content-Type (use o charset apropriado)
header("Content-Type: text/html; charset=ISO-8859-1\r\n");

// e finalmente liberamos o conteúdo do buffer
ob_end_flush();
    
05.02.2014 / 21:08