0

I am trying to send SOAP request to my server from PHP script. the soap template provided by our vendor is like:

 <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
  xmlns:q0="http://nsn.com/ossbss/charge.once/wsdl/entity/Tis/xsd/1"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:wsa="http://www.w3.org/2005/08/addressing"
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Header>
    <wsse:Security>
      <wsse:UsernameToken>
        <wsse:Username>$USER_NAME</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">$USER_PASSWORD</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>

    <soapenv:Body>
        <q0:CommandRequestData>
          <q0:Environment>
            <q0:Parameter name="ApplicationDomain" value="CAO_LDM_00" />
            <q0:Parameter name="DefaultOperationNamespace" value="GMF" />
          </q0:Environment>
           <q0:Command>
    <q0:Transaction>  
    <!-- read the Balance of a customer -->
        <q0:Operation name="Read" modifier="ROP">
            <q0:ParameterList>
                <q0:StringParameter name="CustomerId">$NUMBER</q0:StringParameter>
                <q0:SymbolicParameter namespace="@" name="SelectionDate">NOW</q0:SymbolicParameter>
            </q0:ParameterList>

            <q0:OutputSpecification>
                <q0:StructParam namespace="GDM" name="Customer">>
                    <q0:SimpleParam name="CustomerId" />
                </q0:StructParam>
                <q0:SimpleParam name="OnPeakAccountID_FU" />
            </q0:OutputSpecification>
        </q0:Operation>
    </q0:Transaction>  
           </q0:Command>
        </q0:CommandRequestData>
    </soapenv:Body>
</soapenv:Envelope>

Here the Username and password both have to be sent on the header, $NUMBER is in the body. and ask for CustomerID and OnPeakAccountID_FU parameters in the response. I want to use curl for performing this post request BUT don't know how to format the request itself. i.e. the WDL.

EDIT: the http request goes to our internal server like:

http://IP:PORT/TisService
Fshamri
  • 1,346
  • 4
  • 17
  • 32
  • php has a built-in [soapclient](http://php.net/manual/en/class.soapclient.php) class, that might be easier. Are you sure you want to talk to a soap api with curl? – Peter Rakmanyi Aug 08 '17 at 11:22
  • http://nsn.com/ossbss/charge.once/wsdl/entity/Tis/xsd/1 is 404 – Peter Rakmanyi Aug 08 '17 at 11:25
  • I have read some recommendation to use cURL. Anyway, this is not an issue, I just want to know how to format my request and pass login details in the header and request for response – Fshamri Aug 08 '17 at 11:26
  • it is private url. nsn is our vendor. I have update the my question – Fshamri Aug 08 '17 at 11:28

1 Answers1

1

You can set up a soapclient like this:

class some_service {
  private function securityheader($username, $password){
    $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
    $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    $root = new SimpleXMLElement('<root/>');
    $root->registerXPathNamespace('wsse', $ns_wsse);
    $security = $root->addChild('wsse:Security', null, $ns_wsse);
    $usernameToken = $security->addChild('wsse:UsernameToken', null, $ns_wsse);
    $usernameToken->addChild('wsse:Username', $username, $ns_wsse);
    $usernameToken->addChild('wsse:Password', $password, $ns_wsse)->addAttribute('Type', $password_type);
    $auth = $root->xpath('/root/wsse:Security')[0]->asXML();
    return new SoapHeader($ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true);
  }

  function __construct($username, $password){
    //assuming this is the wsdl file
    $this->client = new SoapClient('http://nsn.com/ossbss/charge.once/wsdl/entity/Tis/xsd/1', array('trace' => true));
    $this->client->__setSoapHeaders($this->securityheader($username, $password));
  }
}

$USER_NAME = 'username';
$USER_PASSWORD = 'password';

$service = new some_service($USER_NAME, $USER_PASSWORD);

The headers look fine, but I couldn't test it against the service you are using.

You can make a call like this:

$response = $service->client->CommandRequestData($some_data_object);
print_r($response);

and to see the xml sent, do this:

print_r($service->client->__getLastRequest());

Without the wsdl I don't know how you should arrange the data in $some_data_object. What worries me is your sample xml does not look like an object translated into xml, but has attributes on some properties and that complexity is not necessary. Maybe you can see how the wsdl expects the data, using a wsdl viewer.

It is possible the data is expected in this way, or the documentation might not match the wsdl.

Peter Rakmanyi
  • 1,475
  • 11
  • 13
  • Thanks @Peter Rakmanyi your solution was very helpful and it solved my problem, i was having issue to generate wsse headers for SOAP API, Now using this code now i am able to send customized headers along with `wsse:UsernameToken` information. – engr.waqas Aug 16 '17 at 14:14
  • @engr.waqas You can also extend the soapheader class to create it like in [this answer](https://stackoverflow.com/questions/953639/connecting-to-ws-security-protected-web-service-with-php/6677930#6677930). – Peter Rakmanyi Aug 16 '17 at 15:01