在PHP中无法理解SOAP

Having trouble getting my head around SOAP in PHP

提问人:Andrew G. Johnson 提问时间:6/22/2009 最后编辑:Andrew G. Johnson 更新时间:12/10/2009 访问量:4179

问:

好吧,这是我尝试使用的 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();
}

Anywho 这会引发异常并回显以下内容:

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

注意:我以前从未使用过SOAP,所以有可能我只是做了一些根本性错误的事情,即使是让我朝着正确方向前进的小技巧也会不胜感激

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();
}
PHP 肥皂

评论

0赞 Tom Haigh 6/22/2009
我认为您更新的示例仍然缺少一个数组。

答:

4赞 Tom Haigh 6/22/2009 #1

我发现,当使用PHP的SOAP实现时,你最终会把所有东西都包装在比你想象的更多的数组中。

下面的示例似乎有效,但您还需要正确设置日期值的格式,然后才能正常工作。我不确定最好的方法 - 可能是您可以传递一个表示 UNIX 时间的 Integer,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'
            ) 
        ) 
    )
);

评论

0赞 Andrew G. Johnson 6/22/2009
对我不起作用,仍然遇到同样的错误。我编辑了问题以显示我尝试过的内容
0赞 Andrew G. Johnson 6/23/2009
啊,很好的电话,你的评论,我只有 2 个嵌套数组,而不是 3 个——错过了一个,因为你的例子中没有换行符。多谢!
1赞 Marc Bernier 6/23/2009 #2

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

0赞 Nick R. 7/12/2009 #3

尝试创建一个 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);