0

I'm having some trouble creating a connection from a PHP app to a .NET web service. I've read on quite a few websites what to do but I cannot seem to make it work.

The web service requires a username and password and all I am trying to do is to get some user profile data in a user data management system by sending the user's ID.

This is what the provider says the SOAP request should look like:

POST /feed30/clientdataservice.asmx HTTP/1.1
Host: ws-ic-pilot.acme.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "GetUser"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUser xmlns="urn:Acme:ClientDataService">
  <strUserId>string</strUserId>
</GetUser>
</soap:Body>
</soap:Envelope>

Here's the code I have written based on what I found on various sites on the internet:

$client = new SoapClient('https://ws-ic-pilot.acme.com/feed30/clientdataservice.asmx
WSDL',Array("trace" => 1));
$header = new SoapHeader(
    array(
            'UserName' => 'myusername',
            'Password' => 'mypassword',
            'strUserID' => '12345'
    ) );

$client->__setSoapHeaders($header);

$client->GetUser();

When I used __getLastRequestHeaders and __getLastRequest the SOAP message that I am creating looks like this:

POST /feed30/clientdataservice.asmx HTTP/1.1
Host: ws-pilot.acme.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.21
Content-Type: text/xml; charset=utf-8
SOAPAction: "GetUser"
Content-Length: 247

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:Acme:ClientDataService"><SOAP-ENV:Header/>
<SOAP-ENV:Body><ns1:GetUser/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

When I post the SOAP message to the .NET server I get the following response:

<ExceptionInfo>Exception Message:       Security requirements are not satisfied because the  
security header is not present in the incoming message.;Exception Target Method:
 ValidateMessageSecurity</ExceptionInfo></detail>
</soap:Body></soap:Envelope>

Can anyone provide a suggestion/recommendation as to how I fix this issue?

Thanks in advance for your assistance!

mb87
  • 119
  • 1
  • 10

1 Answers1

0

So I found the answer to my question. Seems that SOAPClient was not the best way to do this in my case. Instead a better way to do this is to create, send and receive a SOAP message by CURL. Below is an example of what I found on another Stackoverflow page. I adapted it to my situation and it worked great!

$credentials = "username:pass"; 
$url = "https://url/folder/sample.wsdl";
$body = ''; /// Your SOAP XML needs to be in this variable

$headers = array( 
'Content-Type: text/xml; charset="utf-8"', 
'Content-Length: '.strlen($body), 
'Accept: text/xml', 
'Cache-Control: no-cache', 
'Pragma: no-cache', 
'SOAPAction: "customerSearch"'
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);

$data = curl_exec($ch); 
Community
  • 1
  • 1
mb87
  • 119
  • 1
  • 10