Having trouble getting my head around SOAP in PHP

后端 未结 3 881
挽巷
挽巷 2020-12-17 05:51

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          


        
相关标签:
3条回答
  • 2020-12-17 05:56

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

    0 讨论(0)
  • 2020-12-17 06:13

    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);
    
    0 讨论(0)
  • 2020-12-17 06:15

    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'
                ) 
            ) 
        )
    );
    
    0 讨论(0)
提交回复
热议问题