tags:

views:

549

answers:

4

Well here is the API I'm trying to use: http://www.hotelscombined.com/api/LiveRates.asmx?op=HotelSearch

Here is the code I've tried:

$client = new SoapClient('http://www.hotelscombined.com/api/LiveRates.asmx?WSDL');

echo '<pre>'; var_dump($client->__getFunctions()); echo '</pre><br /><br /><br />'; 
//since the above line returns the functions I am assuming everything is fine but until this point

try
{
    $client->__soapCall('HotelSearch',
        array(
            'ApiKey' => 'THE_API_KEY_GOES_HERE', // note that in the actual code I put the API key in...
            'UserID' => session_id(),
            'UserAgent' => $_SERVER['HTTP_USER_AGENT'],
            'UserIPAddress' => $_SERVER['REMOTE_ADDR'],
            'HotelID' => '50563',
            'Checkin' => '07/02/2009',
            'Checkout' => '07/03/2009',
            'Guests' => '2',
            'Rooms' => '1',
            'LanguageCode' => 'en',
            'DisplayCurrency' => 'usd',
            'TimeOutInSeconds' => '90'
        )
    );
}
catch (Exception $e)
{
    echo $e->getMessage();
}

Anywho this throws an exception and echos the following:

Server was unable to process request. ---> Object reference not set to an instance of an object.

NOTE: I've never used SOAP before so it's possible I'm just doing something fundamentally wrong, even a small tip to get me in the right direction would be hugely appreciated

Tom Haigh suggested wrapping the values in another array which seems to be returning the same error message: (I always tried changing integers to be in integer form and the same with dates)

try
{
    $client->__soapCall('HotelSearch',
        array('request' =>
        array(
            'ApiKey' => 'THE_API_KEY_GOES_HERE', // note that in the actual code I put the API key in...
            'UserID' => session_id(),
            'UserAgent' => $_SERVER['HTTP_USER_AGENT'],
            'UserIPAddress' => $_SERVER['REMOTE_ADDR'],
            'HotelID' => '50563',
            'Checkin' => '2009-07-02',
            'Checkout' => '2009-07-03',
            'Guests' => 2,
            'Rooms' => 1,
            'LanguageCode' => 'en',
            'DisplayCurrency' => 'usd',
            'TimeOutInSeconds' => 90
        ) )
    );
}
catch (Exception $e)
{
    echo $e->getMessage();
}
+3  A: 

I find when using PHP's SOAP implementation you end up wrapping everything up in more arrays than you think you need.

The below example seems to work, but also you need to format your date values correctly before it will work. I'm not sure of the best way of doing this - it might be that you can pass an Integer representing UNIX time and PHP will convert it for you.

$client->__soapCall('HotelSearch', 
    array(
        array('request' => 
            array(
                'ApiKey' => 'THE_API_KEY_GOES_HERE', // note that in the actual code I put the API key in...
                'UserID' => session_id(),
                'UserAgent' => $_SERVER['HTTP_USER_AGENT'],
                'UserIPAddress' => $_SERVER['REMOTE_ADDR'],
                'HotelID' => '50563',
                'Checkin' => '07/02/2009',
                'Checkout' => '07/03/2009',
                'Guests' => '2',
                'Rooms' => '1',
                'LanguageCode' => 'en',
                'DisplayCurrency' => 'usd',
                'TimeOutInSeconds' => '90'
            ) 
        ) 
    )
);
Tom Haigh
Didn't work for me, still getting the same error. I edited the question to show what I tried
Andrew G. Johnson
Ahh good call on your comment, I was only 2 nested arrays deep instead of 3 -- missed one because there wasn't a line break in your example. Thanks a lot!
Andrew G. Johnson
+1  A: 

One thing that drove me crazy for days - double-check the names of your array elements (ApiKey, UserId, etc). Make sure the case is correct also. I wasted hours on a on an incorrectly cased 'm'.

Marc Bernier
A: 

Try making a PHP object, then referencing that object in your soap call.

class HotelRequest {
   public $apiKey;
   public $userID;
   public $userAgent;
   public $userIPAddress;
   public $hotelID;
   public $checkin;
   public $checkout;
   public $guests;
   public $rooms;
   public $languageCode;
   public $displayCurrency;
   public $timeOutInSeconds;  
}

//set the values of the object...
$hotelRequestObject = new HotelRequest();
$hotelRequestObject->apiKey = "API_KEY";
//etc...

$client = new SoapClient('http://www.hotelscombined.com/api/LiveRates.asmx?WSDL',
    array("classmap" => array("HotelSearchRequest" => "HotelRequest")));

$result = $client->HotelSearch($hotelRequestObject);

var_dump($result);
Nick R.
A: 

I got the API from http://www.sinohotel.com to support our hotel channel, seems very good, you can check it from our site - 51tour

china hotels