从 PHP 连接到 SOAP 服务器的“未将对象引用设置为对象的实例”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6541663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Object reference not set to an instance of an object" error connecting to SOAP server from PHP
提问by wonder95
I'm making my first attempt to connect to a SOAP server from PHP, and I'm not understanding how to log in and get the data I need. The service I'm trying to connect to is the Hawley USA service http://hawleyusa.com/thcServices/StoreServices.asmx). I've been looking at a few posts on how to connect, and I get the basics. I've verified that I have SOAP enabled in my PHP, and I'm just trying to get an inventory list. Here's the code I'm using:
我第一次尝试从 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;
}
However, when I run this code, I get this error:
但是,当我运行此代码时,出现此错误:
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
When debugging, I can see the $client instance initiated, so I'm not sure why I'm getting this error.
调试时,我可以看到 $client 实例已启动,因此我不确定为什么会收到此错误。
Second question: Am I passing the user ID and password correctly?
第二个问题:我是否正确传递了用户 ID 和密码?
Thanks.
谢谢。
Update: I threw in $client->__getLastRequest, and this is what I got:
更新:我输入了 $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>
So I can see that I'm missing my login ID and password. How do I add them to my InventoryList call?
所以我可以看到我丢失了我的登录 ID 和密码。如何将它们添加到我的 InventoryList 调用中?
回答by Jonathan
You're close. Looking at the WSDL the InventoryList method takes an object called "request". Modify your call line slightly:
你很接近。查看 WSDL,InventoryList 方法采用一个名为“请求”的对象。稍微修改您的呼叫线路:
$client->InventoryList(array("request" => array("LoginId" => $login_id, "Password" => $password));
回答by darkstar_mx
Probably it's not the same case but it also gives the same error if you don't specify empty strings in fields you don't need to use, taken from http://www.sitepoint.com/forums/showthread.php?755549-SOAP-XML-Object-reference-not-set-to-an-instance-of-an-object
可能情况不同,但如果您没有在不需要使用的字段中指定空字符串,它也会出现相同的错误,取自http://www.sitepoint.com/forums/showthread.php?755549 -SOAP-XML-Object-reference-not-set-to-an-instance-of-an-object