在PHP中理解SOAP时遇到困难
下面是我尝试使用的API: 以下是我尝试过的代码:在PHP中理解SOAP时遇到困难,php,soap,Php,Soap,下面是我尝试使用的API: 以下是我尝试过的代码: $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
$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();
}
注意:我以前从未使用过肥皂,所以我可能只是做了一些根本错误的事情,即使是一个小提示,让我朝着正确的方向走,我也会非常感激
Tom Haigh建议将这些值包装到另一个数组中,该数组似乎返回相同的错误消息:(我总是尝试将整数更改为整数形式,并且与日期相同)
我发现,当使用PHP的SOAP实现时,最终将所有内容封装在比您认为需要的更多的数组中 下面的示例似乎可行,但您还需要正确设置日期值的格式,然后才能正常工作。我不确定这样做的最佳方式——可能是您可以传递一个表示UNIX时间的整数,PHP将为您转换它
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);
有一件事让我疯狂了好几天——仔细检查数组元素的名称(ApiKey、UserId等)。确保案例也正确无误。我在一个大小写不正确的'm'上浪费了几个小时。尝试创建一个PHP对象,然后在soap调用中引用该对象
对我来说不起作用,仍然得到同样的错误。我编辑了这个问题以显示我对您的评论的看法,我只使用了2个嵌套数组,而不是3个--遗漏了一个,因为您的示例中没有换行符。谢谢!我认为您更新的示例仍然缺少一个数组。
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();
}
$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'
)
)
)
);
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);