代码之家  ›  专栏  ›  技术社区  ›  Andrew G. Johnson

在PHP中理解SOAP时遇到困难

  •  3
  • Andrew G. Johnson  · 技术社区  · 16 年前

    下面是我尝试使用的API: http://www.hotelscombined.com/api/LiveRates.asmx?op=HotelSearch

    以下是我尝试过的代码:

    $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();
    }
    

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

    注意:我以前从未使用过肥皂,所以我可能只是做了一些根本错误的事情,即使是一个小提示,让我朝着正确的方向走,我也会非常感激

    Tom Haigh建议将这些值包装到另一个数组中,该数组似乎返回相同的错误消息:(我总是尝试将整数更改为整数形式,并且与日期相同)

    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 回复  |  直到 16 年前
        1
  •  4
  •   Tom Haigh    16 年前

    下面的示例似乎可行,但您还需要正确设置日期值的格式,然后才能正常工作。我不确定这样做的最佳方式——可能是您可以传递一个表示UNIX时间的整数,PHP将为您转换它。

    $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'
                ) 
            ) 
        )
    );
    
        2
  •  1
  •   Marc Bernier    16 年前

    有一件事让我疯狂了好几天——仔细检查数组元素的名称(ApiKey、UserId等)。确保案例也正确无误。我在一个错误的“m”上浪费了几个小时。

        3
  •  0
  •   Nick R.    15 年前

    尝试创建一个PHP对象,然后在soap调用中引用该对象。

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