无法理解 PHP 中的 SOAP
这是我尝试使用的 API:http://www.hotelscombined.com/api/LiveRates.asmx?op=HotelSearch
这是我试过的代码:
$client = new SoapClient('http://www.hotelscombined.com/api/LiveRates.asmx?WSDL');echo '';var_dump($client->__getFunctions());回声'</pre><br/><br/><br/>';//由于上面的行返回函数我假设一切都很好,但直到这一点尝试{$client->__soapCall('HotelSearch',大批('ApiKey' =>'THE_API_KEY_GOES_HERE',//请注意,在实际代码中我将 API 密钥放在...'用户ID' =>session_id(),'用户代理' =>$_SERVER['HTTP_USER_AGENT'],'用户IP地址' =>$_SERVER['REMOTE_ADDR'],'酒店ID' =>'50563','签到' =>'07/02/2009','结帐' =>'07/03/2009','客人' =>'2','房间' =>'1','语言代码' =>'恩','显示货币' =>'美元','TimeOutInSeconds' =>'90'));}捕获(异常 $e){echo $e->getMessage();}
这会引发异常并回显以下内容:
服务器无法处理请求.--->你调用的对象是空的.
注意:我以前从未使用过 SOAP,所以我可能只是在做一些根本错误的事情,即使是让我朝着正确方向前进的小技巧也会非常感谢
Tom Haigh 建议将值包装在另一个似乎返回相同错误消息的数组中:(我总是尝试将整数更改为整数形式并且与日期相同)
试试{$client->__soapCall('HotelSearch',数组('请求' =>大批('ApiKey' =>'THE_API_KEY_GOES_HERE',//请注意,在实际代码中我将 API 密钥放在...'用户ID' =>session_id(),'用户代理' =>$_SERVER['HTTP_USER_AGENT'],'用户IP地址' =>$_SERVER['REMOTE_ADDR'],'酒店ID' =>'50563','签到' =>'2009-07-02','结帐' =>'2009-07-03','客人' =>2、'房间' =>1、'语言代码' =>'恩','显示货币' =>'美元','TimeOutInSeconds' =>90) ));}捕获(异常 $e){echo $e->getMessage();}
解决方案 我发现在使用 PHP 的 SOAP 实现时,您最终会将所有内容包装在比您认为需要的更多的数组中.
下面的示例似乎有效,但您还需要先正确设置日期值的格式才能起作用.我不确定这样做的最佳方法 - 可能是您可以传递一个代表 UNIX 时间的整数,PHP 会为您转换它.
$client->__soapCall('HotelSearch',大批(数组('请求' =>大批('ApiKey' =>'THE_API_KEY_GOES_HERE',//请注意,在实际代码中我将 API 密钥放在...'用户ID' =>session_id(),'用户代理' =>$_SERVER['HTTP_USER_AGENT'],'用户IP地址' =>$_SERVER['REMOTE_ADDR'],'酒店ID' =>'50563','签到' =>'07/02/2009','结帐' =>'07/03/2009','客人' =>'2','房间' =>'1','语言代码' =>'恩','显示货币' =>'美元','TimeOutInSeconds' =>'90'))));
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();
}
解决方案
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'
)
)
)
);
相关文章