我第一次尝试从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-&gt; __ 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调用中?
答案 0 :(得分:24)
你很亲密。查看WSDL,InventoryList方法接受一个名为“request”的对象。稍微修改一下你的电话:
$client->InventoryList(array("request" => array("LoginId" => $login_id, "Password" => $password));
答案 1 :(得分:1)
可能情况并非相同,但如果您未在http://www.sitepoint.com/forums/showthread.php?755549-SOAP-XML-Object-reference-not-set-to-an-instance-of-an-object <来自不需要使用的字段中指定空字符串,也会出现同样的错误/ p>