1

I have created simple SOAP API which is as mentioned below.

catalog.wsdl

<?xml version ='1.0' encoding='UTF-8' ?> 
 <definitions name='Catalog' 
  targetNamespace='http://example.org/catalog' 
  xmlns:tns=' http://example.org/catalog ' 
  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='getUserRequest'> 
     <part name='catalogId' type='xsd:string'/> 
  </message> 
  <message name='getUserResponse'> 
     <part name='Result' type='xsd:string'/> 
  </message> 

   <portType name='UserPortType'> 
      <operation name='getUser'> 
          <input message='tns:getUserRequest'/> 
          <output message='tns:getUserResponse'/> 
      </operation> 
   </portType> 

   <binding name='UserBinding' type='tns:UserPortType'> 
      <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http' /> 
      <operation name='getUser'> 
          <soap:operation soapAction='urn:localhost-user#getUser'/> 
          <input> 
            <soap:body use='encoded' namespace='urn:localhost-user' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>     
          </input> 
          <output> 
            <soap:body use='encoded' namespace='urn:localhost-user' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
           </output> 
        </operation> 
    </binding> 

    <service name='UserService'> 
        <port name='UserPort' binding='UserBinding'> 
          <soap:address location='http://localhost/soap/soap-server.php'/> 
        </port> 
    </service>

soap-server.php

 function getUser($catalogId) { 
      if($catalogId=='catalog1'){
           return "hello";    
      }
  }
  ini_set("soap.wsdl_cache_enabled", "0"); 
  $server = new SoapServer("catalog.wsdl"); 
  $server->addFunction("getUser"); 
  $server->handle(); 

soap-client.php

  $client = new SoapClient('catalog.wsdl');
  $catalogId='catalog2';
  $response = $client->getUser($catalogId);
  echo $response;

Now I want to add SOAP Authentication in this. How to do it? can anyone provide me code?

Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
  • How about providing a SOAP service method `login` which takes to params `username` and `password`. – TiMESPLiNTER Nov 26 '13 at 14:13
  • @TiMESPLiNTER : I think you are talking about creating login function and DB authentication. I want to provide HTTP authentication. can you help me with that? – Rukmi Patel Nov 27 '13 at 04:49
  • Propably this can help you: http://stackoverflow.com/questions/10219463/wsdl-to-php-with-basic-auth According to this you just have to provide an array with `login` and `password` element and give it to the constructor of the `SoapClient` as second parameter. – TiMESPLiNTER Nov 27 '13 at 07:13
  • @TiMESPLiNTER : I have gone through link provided. I got the idea how to pass parameters for login but where actually code should be written for authentication? – Rukmi Patel Nov 27 '13 at 12:17
  • You can protect the API with `htaccess` and `htpasswd` if you're on apache. – TiMESPLiNTER Nov 27 '13 at 12:55

0 Answers0