尝试 SOAP 调用时未将对象引用设置为对象错误的实例 [英] Object reference not set to an instance of an object error when trying SOAP call

查看:31
本文介绍了尝试 SOAP 调用时未将对象引用设置为对象错误的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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

以下是我当前的代码:

<?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:

  • 使用自定义类
  • 使用 stdClass
  • 使用SoapVar
  • 使用SoapParam
  • 使用简单的数组
  • 双包裹数组
  • 上述内容的多种组合.

而且我已经尝试调用像 $client->__soapCall('LogOn', [milliontries]) 以及 $client->LogOn([milliontries])

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

...没有任何效果,请帮忙.

...nothing works, please help.

更新:

我终于设法得到一个不同的错误(这表明至少我发现了一些稍微不那么错误的错误).我的代码现在看起来像这样:

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')));

这给了我一个关于 LogOnType 不受支持的错误(丹麦语,这表明至少我现在与服务器有某种连接).用户名和密码数组没有影响,我可以替换一个空字符串并得到相同的结果,不同的是小写的logon.

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.

据我所知,wsdl 提供了执行此操作所需的所有信息,但我对 [your_pick] 的了解太多了……?

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...?

推荐答案

难以置信!这16行代码只用了9个小时.

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);

我多次如此接近,结果发现命名空间(URL)是我遗漏的神奇部分.在每次使用 SoapVar 的尝试中,我一直在使用主 wsdl 中的命名空间(或根本没有命名空间).

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

这篇关于尝试 SOAP 调用时未将对象引用设置为对象错误的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆