Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object reference not set to an instance of an object error when trying SOAP call

Tags:

php

soap

wsdl

I am trying to connect to and authenticate with a webservice using SOAP / wsdl, but I constantly get the error:

<b>Fatal error</b>:  Uncaught SoapFault exception: [a:InternalServiceFault] Object reference not set to an instance of an object. in /path/install.php:16

Below is my current code:

<?php
header("Content-Type: text/plain"); 

$userinfo = array(
    'Username'=>'test',
    'Password'=>'test'
);

$wsdl_url = 'https://ws-e-distribution.kmd.dk/standard/ServiceAutorisation/ServiceAutorisation.svc?wsdl';
$client = new SoapClient($wsdl_url);

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

//This is the problematic line:
$response = $client->__soapCall('LogOn', array('LogOn' => $userinfo));

var_dump($response);

I have tried every possible way of wrapping the username and password parameters that I could conceive of or find anyone suggesting:

  • using a custom class
  • using a stdClass
  • using SoapVar
  • using SoapParam
  • using a simple array
  • double wrapped array
  • A lot of combinations of the above.

And I've tried calling the function like $client->__soapCall('LogOn', [milliontries]) as well as $client->LogOn([milliontries])

...nothing works, please help.

Update:

I finally managed to get a different error (that suggests at least I hit upon something slightly less wrong). My code now looks like this:

$response = $client->LogOn(array('logon' => array('Username' => 'test','Password' => 'test')));

Which gives me an error about the LogOnType being unsupported (in Danish, which suggests to me that at least I have some sort of connection to the server now). The username and password array has no effect, I can substitute an empty string and get the same result, the thing that makes the difference is the lowercase logon.

If anyone can set up a working example that gives the error incorrect username or password I will be forever grateful... but any pointers will be much appreciated.

As far as I understand the wsdl gives all the information needed to do this, I'm just too much of a [your_pick] to get it...?

like image 318
Mikk3lRo Avatar asked Jul 04 '14 12:07

Mikk3lRo


1 Answers

Unbelievable ! It only took nine hours to write these 16 lines of code.

<?php
header("Content-Type: text/plain"); 

$userinfo = array(
    'Username'=>'test',
    'Password'=>'test'
);

$wsdl_url = 'https://ws-e-distribution.kmd.dk/standard/ServiceAutorisation/ServiceAutorisation.svc?wsdl';
$client = new SoapClient($wsdl_url, array('trace' => 1, "exception" => 0));

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

//This actually works ! :) :) :)
$response = $client->LogOn(array('logon' => new SoapVar($userinfo, SOAP_ENC_OBJECT, 'LogOnUsername', 'http://schemas.datacontract.org/2004/07/WebServiceAutorisation.BL')));

var_dump($response);

I was so close several times, and it turned out the namespace (the URL) was the magic bit I was missing. I had been using the namespace from the main wsdl (or no namespace at all) in every attempt at using SoapVar's.

Well now, on to the actual purpose of logging in, probably won't be any easier

like image 185
Mikk3lRo Avatar answered Nov 08 '22 13:11

Mikk3lRo