难以理解PHP中的SOAP [英] Having trouble getting my head around SOAP in PHP
问题描述
以下是我要使用的API: http://www.hotelscombined.com/api/LiveRates.asmx?op=HotelSearch
Well here is the API I'm trying to use: 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 this throws an exception and echos the following:
Server was unable to process request. ---> Object reference not set to an instance of an object.
注意:我以前从未使用过SOAP,所以很可能我只是在做一些根本上错误的事情,即使是向我介绍正确方向的小技巧也将受到高度赞赏
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建议将值包装在另一个似乎返回相同错误消息的数组中:(我一直尝试将整数更改为整数形式,并与日期相同)
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();
}
推荐答案
我发现,使用PHP的SOAP实现时,您最终将所有内容包装在比您认为需要的更多数组中.
I find when using PHP's SOAP implementation you end up wrapping everything up in more arrays than you think you need.
以下示例似乎有效,但是您还需要正确设置日期值的格式才能生效.我不确定执行此操作的最佳方法-可能是您可以传递代表UNIX时间的Integer,而PHP会为您转换它.
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'
)
)
)
);
这篇关于难以理解PHP中的SOAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!