Question

Hey guys I just started to learn soap and I got a problem when I'm expecting to receive a integer all good and when I'm expecting to receive a string or array or json , error is :

SOAP-ERROR: Encoding: Violation of encoding rules

I already seen all answers about this and it is not helping me. Any ideas what problem is?

Thanks for any advice.

My code is :

  <?xml version='1.0' encoding='UTF-8' ?>
   <definitions name='Names'
             targetNamespace='http://localhost/soap'
             xmlns:tns=' http://localhost/soap '
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
            xmlns:xsd='http://www.w3.org/2001/XMLSchema'
            xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
            xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
         xmlns='http://schemas.xmlsoap.org/wsdl/'>

        <message name="getNamesRequest">
            <part name="num" type="xsd:string"/>
        </message>
        <message name="getNamesResponse">
            <part name="Result" type="xsd:string"/>
        </message>

        <portType name="NamesPortType">
           <operation name="getNames">
             <input message="tns:getNamesRequest"/>
             <output message="tns:getNamesResponse"/>
           </operation>
        </portType>

        <binding name="NamesBinding" type="tns:NamesPortType">
          <soap:binding style="rpc"
             transport="http://schemas.xmlsoap.org/soap/http"/>
              <operation name="getNames" />
        </binding>

         <service name="NamesService">
            <port name="NamesPort" binding="tns:NamesBinding" >
             <soap:address
                location="http://localhost/soap/server.php" />
             </port>
         </service>
    </definitions>

server.php

     <?php

       function getNames($num)
        {

          $name = $num;
          return $name;
         }

       ini_set('soap.wsdl_cache_enabled', '0');

         $server = new SoapServer("http://localhost/soap/names.wsdl");

          $server->addFunction("getNames");

           $server->handle();

client.php

          <?php

              try {
                 $client = new SoapClient('http://localhost/soap/names.wsdl');
                 $result = $client->getNames('victor');

                 echo $result;
              } catch (SoapFault $e) {
                  echo $e->getMessage();
              }
Was it helpful?

Solution

Please initialize your server and client with UTF-8 encoding:

$server = new SoapServer('http://localhost/soap/names.wsdl', array('encoding'=>'UTF-8'));
$client = new SoapClient('http://localhost/soap/names.wsdl', array('encoding'=>'UTF-8'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top