";对象引用未设置为对象的实例“;从PHP连接到SOAP服务器时出错


"Object reference not set to an instance of an object" error connecting to SOAP server from PHP

我第一次尝试从PHP连接到SOAP服务器,但我不知道如何登录并获取所需的数据。我要连接的服务是Hawley USA服务http://hawleyusa.com/thcServices/StoreServices.asmx)。我一直在看一些关于如何连接的帖子,我了解了一些基本知识。我已经验证了我的PHP中启用了SOAP,我只是想获得一个清单列表。这是我正在使用的代码:

<?php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$wsdl_path = "http://hawleyusa.com/thcServices/StoreServices.asmx?WSDL";
$login_id = 'mylogin_id';
$password = 'mypassword';
$client = new SoapClient($wsdl_path);
try {
  echo "<pre>'n";
  print($client->InventoryList(array("LoginID" => $login_id, "Password" => $password)));
  echo "'n";
}
catch (SoapFault $exception) {
  echo $exception;      
} 

然而,当我运行此代码时,我会得到以下错误:

SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object. in /Users/steve/Sites/mysite/hawley_client.php:12

调试时,我可以看到$client实例已启动,所以我不确定为什么会出现此错误。

第二个问题:我是否正确地传递了用户ID和密码?

谢谢。

更新:我加入了$client->__getLastRequest,这就是我得到的:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"              xmlns:ns1="http://hawleyusa.com/thcServices/">
<SOAP-ENV:Body>
<ns1:InventoryList/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

所以我可以看到我的登录ID和密码不见了。如何将它们添加到InventoryList调用中?

你很接近。在WSDL中,InventoryList方法接受一个名为"request"的对象。稍微修改您的电话线:

$client->InventoryList(array("request" => array("LoginId" => $login_id, "Password" => $password));

可能情况不同,但如果您不在不需要使用的字段中指定空字符串,也会出现相同的错误http://www.sitepoint.com/forums/showthread.php?755549-SOAP XML对象引用未设置为对象的实例

在我的案例中,问题是打字错误:

在给定的文档中,文件名为documentShipmentAddress(我使用的是这个)

但在wdsl(模式)中为:

shipmentAddress

所以这可能是这个错误消息的问题。我已经将字段名称更改为shipmentAddress,它解决了问题。