PHP SOAP 问题:未将对象引用设置为对象的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/915256/
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
PHP SOAP Issue : Object reference not set to an instance of an object
提问by
I'm trying to build an interface to https://ws.farebuzz.com/FlightGateway.asmx?WSDLusing php and SoapClient class.
我正在尝试使用 php 和 SoapClient 类构建一个到https://ws.farebuzz.com/FlightGateway.asmx?WSDL的接口。
I managed to get over the authentication header but I'm stuck when I try to call a method .. I'm always getting :
我设法克服了身份验证标头,但是当我尝试调用方法时卡住了..我总是得到:
Uncaught SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object.
未捕获的 SoapFault 异常:[soap:Server] 服务器无法处理请求。---> 未将对象引用设置为对象的实例。
I tried calling it like this (as an object) :
我试着这样称呼它(作为一个对象):
class SearchFlights{
public $NumberOfAdults;
public $ClassOfService;
public $TypeOfTrip;
public $FromCity;
public $ToCity;
}
$parameters = new SearchFlights();
$parameters->NumberOfAdults = 2;
$parameters->ClassOfService = 'ECONOMY';
$parameters->FromCity = 'ECONOMY';
$parameters->ToCity = '1te';
$parameters->TypeOfTrip = 'NONE';
$this->client->SearchFlights($parameters);
and as an array like :
并作为一个数组,如:
$parameters = array('ToCity' => 'testttt',...);
but I got same error. Can anyone help?
但我遇到了同样的错误。任何人都可以帮忙吗?
Thanks
谢谢
Sorin
索林
回答by MrCode
Your WSDL states that it has to be called in this way:
您的 WSDL 声明必须以这种方式调用它:
$this->client->SearchFlights(array('searchRequest' => $parameters));
Note that the searchRequestname is important.
请注意,searchRequest名称很重要。
There are also various mandatory inputs for that service, you should ensure that those are present. From looking at your code, you are missing some.
该服务还有各种强制性输入,您应该确保它们存在。从查看您的代码来看,您错过了一些。
Take a look at the FlightSearchRequestcomplex type here. Each param that has minOccurs=1is required.
看看FlightSearchRequest这里的复杂类型。每个具有的参数minOccurs=1都是必需的。
<s:complexType name="FlightSearchRequest">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="TypeOfTrip" type="tns:TripType"/>
<s:element minOccurs="0" maxOccurs="1" name="FromCity" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="ToCity" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="ReturnFromCity" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="ReturnToCity" type="s:string"/>
<s:element minOccurs="1" maxOccurs="1" name="DepartureDate" type="s:dateTime"/>
<s:element minOccurs="1" maxOccurs="1" name="ReturnDate" type="s:dateTime"/>
<s:element minOccurs="1" maxOccurs="1" name="NumberOfAdults" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="NumberOfChildren" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="NumberOfInfantsInLap" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="NumberOfInfantsOnSeat" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="NumberOfSeniors" type="s:int"/>
<s:element minOccurs="0" maxOccurs="1" name="AirlinePreference" type="s:string"/>
<s:element minOccurs="1" maxOccurs="1" name="ClassOfService" type="tns:FlightClass"/>
<s:element minOccurs="1" maxOccurs="1" name="IsNonStop" type="s:boolean"/>
<s:element minOccurs="1" maxOccurs="1" name="ConsolidatorFaresOnly" type="s:boolean"/>
<s:element minOccurs="0" maxOccurs="1" name="FpAffiliate" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="FpSubAffiliate" type="s:string"/>
</s:sequence>
</s:complexType>
回答by wajiw
Try using this:
尝试使用这个:
$this->client->SearchFlights(array('parameters' => $parameters));
I was having problems trying to access a .net webservice and this solved it for me.
我在尝试访问 .net 网络服务时遇到问题,这为我解决了问题。
回答by Bjarte Aune Olsen
If I understand the WSDL correctly, the SearchFlights object is supposed to contain a FlightSearchRequest object. It is the latter that contains the parameters.
如果我正确理解 WSDL,则 SearchFlights 对象应该包含 FlightSearchRequest 对象。后者包含参数。
Try this:
尝试这个:
$parameters->FlightSearchRequest->NumberOfAdults = 2;
$parameters->FlightSearchRequest->ClassOfService = 'ECONOMY';
// etc...
$this->client->SearchFlights($parameters);
回答by RishiM
I was also curious to know the answer of this question and finally i got to know the reason of this: The nodes which we are sending through SOAP request should be known to us with Data types and whether they are compulsory or not. So here if any of these nodes followed by strict instructions of that WSDL shouldn't be followed we will get the error which will say that the "Object reference not set to an instance of an object". I will give you an example which i faced: I was having the same issue, and i got to know that i wasn't sending any value to a node, which was excepting at least one value or one occurrence of it, then i validate it on my end, if successful i was sending that value otherwise empty string, which was telling that WSDL that this is at least 1 occurrence and has a value. Finally i got resolved this bug. Baseline here is, if the nodes those are compulsory and did not send perfectly will raise into this exception or else send empty string into those nodes. Thanks
我也很想知道这个问题的答案,最后我知道了这个问题的原因:我们应该知道我们通过 SOAP 请求发送的节点的数据类型以及它们是否是强制性的。因此,如果这些节点中的任何一个不应该遵循该 WSDL 的严格指令,我们将收到错误消息,指出“对象引用未设置为对象的实例”。我会给你一个我遇到的例子:我遇到了同样的问题,我知道我没有向节点发送任何值,除了至少一个值或它的一次出现之外,然后我验证最后,如果成功,我将发送该值,否则为空字符串,这告诉 WSDL 这至少发生了 1 次并且有一个值。最后我解决了这个错误。这里的基线是,如果那些是强制性的并且没有完美发送的节点将引发此异常,或者向这些节点发送空字符串。谢谢

