Error with SOAP webservice using PHP

Asked

Viewed 1,277 times

3

I’m trying to learn how to make one webservice using this tutorial: http://www.sitepoint.com/web-services-with-php-and-soap-2/

But my server is returning the following:

Error

Response not of type text/xml: text/html

This is my client.php:

<?php
    require_once "lib/nusoap.php";
    $client = new nusoap_client("soma.wsdl", true);

    $error = $client->getError();
    if ($error) {
        echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
    }

    $result = $client->call("soma", array("a" => "1", "b" => "2"));

    if ($client->fault) {
        echo "<h2>Fault</h2><pre>";
        print_r($result);
        echo "</pre>";
    }
    else {
        $error = $client->getError();
        if ($error) {
            echo "<h2>Error</h2><pre>" . $error . "</pre>";
        }
        else {
            echo "<h2>Soma</h2><pre>";
            print_r($result);
            echo "</pre>";
        }
    }
?>

And this is my php server.:

<?php
    require_once "lib/nusoap.php";

    function soma($a, $b) {
        return $a + $b;
    }
    $server = new soap_server();
    $server->configureWSDL("resultado", "urn:resultado");

    $server->register("soma",           // nome da função
    array("category" => "xsd:int"),     // tipo de entrada (inteiro, string...)
    array("return" => "xsd:int"),       // tipo de saída
    "urn:resultado",                    // define o namespace
    "urn:resultado#soma",               // define a ação do SOAP
    "rpc",                              // define o tipo da chamada
    "encoded",                          // define o valor para o atributo 'use'
    "Retorna a soma de dois números");  // documentação do que a função faz

    $server->service($HTTP_RAW_POST_DATA);
?>

And finally, this is my sum wsdl.:

<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:resultado" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:resultado">
    <types>
        <xsd:schema targetNamespace="urn:resultado">
            <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
            <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/"/>
        </xsd:schema>
    </types>
    <message name="somaRequest">
        <part name="category" type="xsd:int"/>
    </message>
    <message name="somaResponse">
        <part name="return" type="xsd:int"/>
    </message>
    <portType name="resultadoPortType">
        <operation name="soma">
            <documentation>Retorna a soma de dois números</documentation>
            <input message="tns:somaRequest"/>
            <output message="tns:somaResponse"/>
        </operation>
    </portType>
    <binding name="resultadoBinding" type="tns:resultadoPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="soma">
            <soap:operation soapAction="urn:resultado#soma" style="rpc"/>
            <input>
                <soap:body use="encoded" namespace="urn:resultado" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </input>
            <output>
                <soap:body use="encoded" namespace="urn:resultado" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
            </output>
        </operation>
    </binding>
    <service name="resultado">
        <port name="resultadoPort" binding="tns:resultadoBinding">
            <soap:address location="http://renanlazarotto.com/server/server.php"/>
        </port>
    </service>
</definitions>

I can’t understand where the mistake is. What should I do to make the mistake go away? If I put the webservice address in place of the sum.wsdl, this is the result:

Summing up

3

What is expected from the webservice.

1 answer

1


The expected response type is an XML and it is receiving an HTML.

Response not of type text/xml: text/html

You can consume your service using REST Client for testing, and if so force the php header to return an XML to you.

To add a Header make sure that nothing is going in the header before you add the command, place it after you start <php?

Ex:

<?php header('Content-Type:text/xml');
//Aqui continua o código.
?>

Browser other questions tagged

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