无法理解 PHP 中的 SOAP

Having trouble getting my head around SOAP in PHP(无法理解 PHP 中的 SOAP)
本文介绍了无法理解 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'
            ) 
        ) 
    )
);

这篇关于无法理解 PHP 中的 SOAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)