简体   繁体   中英

PHP SOAP Issue : Object reference not set to an instance of an object

I'm trying to build an interface to https://ws.farebuzz.com/FlightGateway.asmx?WSDL using php and SoapClient class.

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.

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

Your WSDL states that it has to be called in this way:

$this->client->SearchFlights(array('searchRequest' => $parameters));

Note that the searchRequest name is important .

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 FlightSearchRequest complex type here. Each param that has minOccurs=1 is required.

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

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.

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

If I understand the WSDL correctly, the SearchFlights object is supposed to contain a FlightSearchRequest object. It is the latter that contains the parameters.

Try this:

$parameters->FlightSearchRequest->NumberOfAdults = 2;
$parameters->FlightSearchRequest->ClassOfService = 'ECONOMY';
// etc...
$this->client->SearchFlights($parameters);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM