問題描述
這是我嘗試使用的 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'
)
)
)
);
這篇關于無法理解 PHP 中的 SOAP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!