Fshamri
Fshamri

Reputation: 1386

Send SOAP request from PHP

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

Upvotes: 0

Views: 1074

Answers (1)

Peter Rakmanyi
Peter Rakmanyi

Reputation: 1775

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.

Upvotes: 1

Related Questions