Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter to a function with SoapClient?

Unfortunately, I don't have any SOAP experience, please excuse me eventually using wrong notation below -

I have been given 2 URLs and an XML string and I am trying to get an XML-response from a remote SOAP server.

The 1st URL is an WSDL document, I feed it to the SoapClient constructor:

$client = new SoapClient(URL_1, array('trace' => 1, 'exceptions' => 0));

And the 2nd URL is the SOAP server, I use it here:

$client->__setLocation(URL_2);

This works well and I am able to retrieve the types and functions with:

print_r( $client->__getTypes() );
print_r( $client->__getFunctions() );

Here is interesting part of the output:

[28] => struct GetServiceInfo {
 UserAuthentification UserAuthentification;
}

[74] => struct UserAuthentification {
 string UserId;
 string Password;
 string SessionKey;
}

[3] => GetServiceInfoResponse GetServiceInfo(GetServiceInfo $parameters)

My problem is to call the above GetServiceInfo function.

I have been given this XML string which works well with SoapUi program:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://...../V1">
  <soap:Header>.....</soap:Header>   
  <soap:Body>
      <v1:GetServiceInfo>
         <UserAuthentification>
            <UserId>MY_USERNAME</UserId>
            <Password>MY_PASSWORD</Password>
          </UserAuthentification>
      </v1:GetServiceInfo>
   </soap:Body>
</soap:Envelope>

How can I pass this parameter in my PHP script please?

I have tried:

$result = $client->GetServiceInfo(array(
        new SoapParam( "UserAuthentification", array(   # ERROR, WANTS STRING HERE
        new SoapParam( "UserId", "MY_USERNAME" ),
        new SoapParam( "Password", "MY_PASSWORD" ),
)))); 

var_dump( $client->__getLastRequest() );
var_dump( $client->__getLastResponse() );

print_r( $result );

But get the error:

Warning: SoapParam::SoapParam() expects parameter 2 to be string, array given in soap.php

Fatal error: SOAP-ERROR: Encoding: object has no 'UserAuthentification' property in soap.php

I think I am missing something minor here, please help.

UPDATE:

I have come further with the following step:

$user = array('UserId' => 'MY_USERNAME', 'Password' => 'MY_PASSWORD');
$params = array('UserAuthentification' => $user);
$result = $client->GetServiceInfo($params);

Now I get the error:

[FaultDescription] => Unable to identify service

[FaultReason] => wsa:To is missing and the given URL /services could not be mapped to an existing service

like image 920
Alexander Farber Avatar asked Feb 20 '26 00:02

Alexander Farber


1 Answers

The server uses WS-Addressing (as evidenced by the FaultReason - keyword is wsa:To) which is not supported by the regular PHP SoapClient extension.

I have yet to use WS-Addressing but I was recently analysing an API for project we will be working on in the future that requires it.

Please take note of this project or this other project which may be useful (it's easy to search for more PHP WS-Addressing). Again, I have only done research and do not have any hands-on experience to help you with actual code :)

like image 72
Ricardo Velhote Avatar answered Feb 21 '26 15:02

Ricardo Velhote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!