这里是我正在尝试使用的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();
}
答案 0 :(得分:4)
我发现在使用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'
)
)
)
);
答案 1 :(得分:1)
让我疯狂的一件事 - 仔细检查数组元素的名称(ApiKey,UserId等)。确保案例也正确。我在一个错误的“m”上浪费了几个小时。
答案 2 :(得分:0)
尝试制作一个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);