Search code examples
phplaravelsoapsoap-client

Laravel, soap API required login


I am having a little trouble with the SOAP API I need to consume. It requires authorization, so I log in with the following.

Basically, I need to log in with the following, I don't get a "Login Required", which is fine.

$client = new \SoapClient('localwsdlfile.wsdl', array(
    'local_cert' => 'localcert.pem',
    'passphrase' => 'passphrase',
    'location' => 'https://wsmurl/login/'
));

$response = $client->Get(array(
    "AccessKey" => "accesskey",
    "ProductID" => "SOMEPRODUCT",
    "Scope" => "SOMESCOPE",
    "Parameters" => array('Param' => array('_' => 'DATATOLOOKUP', 'id'=>'MOREDATATOLOOKUP'))
));

print_r($response);

So, basically, in the client, I need to uncomment the line

'location' => 'https://wsmurl/login/'

If I don't, I get this login error. The server I am trying to consume has a cache, so I no longer need to send this login for future operations for the next 10 minutes.

So, now, I have logged in, I can go the following, with success.

$client = new \SoapClient('localwsdlfile.wsdl', array(
    'local_cert' => 'localcert.pem',
    'passphrase' => 'passphrase',
    // 'location' => 'https://wsmurl/login/' // Uncomment to login
));

$response = $client->Get(array(
    "AccessKey" => "accesskey",
    "ProductID" => "SOMEPRODUCT",
    "Scope" => "SOMESCOPE",
    "Parameters" => array('Param' => array('_' => 'DATATOLOOKUP', 'id'=>'MOREDATATOLOOKUP'))
));

print_r($response);

If I keep 'location' => 'https://wsmurl/login/' uncommented, I get errors about not sending a valid WSDL.

So in summary:- I am consuming this soap API in Laravel, using PHP's SoapClinet per the above code.

My understanding is that the soap service I am trying to consume required login first. After quite a lot of testing, I managed to get to the point there were no errors by including the line 'location' => 'https://wsmurl/login/' as shown above in the call to SoapClient.

This seems to be required the first time I call this soap service I guess it is logging me in. If I don't include it, I get the error

"SoapFault Login Required"

So, if I include the line 'location' => 'https://wsmurl/login/', it appears to log me in, but I now get the error "SoapFault looks like we got no XML document"

If I keep the line, I continue to get this error "SoapFault looks like we got no XML document"

Removing the line then gives me the expected SOAP response and all is ok.

I have tried catching for errors using try{} ... catch{} looking for exceptions/errors etc, but it isn't catching the "Login Required" or "Looks like we got no XML document" as faults.

So, I guess, is there a better way to detect this condition so I can either send or not without the line or is there a better way to login to the service (note, it just uses the cert and passphrase).

Thanks in advance, I hope the little re-write above makes it clearer.


Solution

  • Because the location for the login and localwsdlfile.wsdl is different, I could not do it with one call. So we created a function using curl to login and if login is successful it will proceed to the soap client. Thanks to Brian from freelancer for his assistance here.

    $client = new SoapClient('wsdl/VocusSchemas/localwsdlfile.wsdl', array(
        'trace' => 1,
        'exception' => true
    ));
    
    try {
    
        $response = $client->Get(array(
            // "AccessKey" => "MADAITABNSAATOT1", 
            "AccessKey" => "accesskey", 
            "ProductID" => "SOMEPRODUCT",
            "Scope" => "SOMESCOPE",
            "Parameters" => array('Param' => array('_' => 'DATATOLOOKUP', 'id' => 'MOREDATATOLOOKUP'))
        ));