Nusoap using Laravel 4

Asked

Viewed 233 times

1

I’m creating a Webservice in the Laravel with the library Noiselabs.

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

So far so good.

Only when I try to show the generated XML with ? wsdl at the end of the URL it gives a problem. Generates a generated XML HTML.

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

When I do with pure PHP it worked.

My client is this way.

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

2 answers

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 (header) that informs the "Content-Type" of the content sent to the browser.

Check which is the Content-Type that the HTTP server is sending to the browser. (Use Firebug or other 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();
  • I did what you said, but it still didn’t work.

0


After some research I managed to solve.

I used Laravel’s Response::make().

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

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.