PHP NuSoap 中的复杂类型 [英] Complex type in PHP NuSoap

查看:34
本文介绍了PHP NuSoap 中的复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP 中的 NuSoap 库构建 Web 服务.我的网络服务将充当客户端和供应商现有网络服务之间的中间层.因此,客户端不是直接连接到供应商,而是连接到我的 Web 服务,我的 Web 服务连接到供应商并获取响应并将相同的响应发送回客户端.

I am building a web service using NuSoap library in PHP. My webservice will act as a middle layer between client and an already existing web service by a vendor. So instead of client connecting to the vendor directly, they will connect to my web service, my web service connects to the vendor and grab the response and send the same response back to the client.

我唯一的问题是我的供应商正在发回 stdclass 对象(他们的网络服务是用 .Net 编写的),我必须接收该对象并通过我的网络服务方法将相同的对象发送回客户端.

My only problem is my vendor is sending back stdclass object (their webservice is written in .Net) and I have to receive that object and send back the same object to the client on my webservice method.

我在互联网上搜索了很多,但没有明确的方法如何通过 NuSoap 库来实现这一点.无论我到目前为止读过什么,指定我必须使用复杂类型来实现这一点,但我再次不知道如何获取响应,然后将其转换为复杂类型并将其发送回客户端.

I have searched quite a bit on Internet but there are no clear ways how to do acheive this by NuSoap library. Whatever I have read so far, specify that I have to use complex type to acheive this, but again I have no clue how to grab the response and then convert it to the complex type and send it back to the client.

预先感谢您的帮助.

推荐答案

您所写的内容被称为 代理.

What you are writing is referred to as a proxy.

在线有 一些示例 NuSoap 服务器发送复杂通过 addComplexType 方法输入.

There are some examples online for NuSoap server sending complex types through the addComplexType method.

//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string')));

实现代理的一种方法是使用存根数据构建您的服务,这样它实际上不会首先与后端服务对话.看看您是否可以让原始客户端对来自代理的人为响应感到满意.然后一旦你有了它,使用真正的后端服务应该是微不足道的(根据我的经验,SOAP 客户端操作比服务器操作更容易).

One approach to implementing the proxy, is build your service with stubbed out data, such that it doesn't actually talk to the backend service first. See if you can get the original client satisfied with a contrived response from your proxy. Then once you have that, consuming the real backend service should be trivial (SOAP client operations are easier than server ones in my experience).

另一种替代方法是考虑使用原生 SoapServer 类.此处的第一条评论显示了如何创建复杂类型.

Another alternative is to consider the native SoapServer class instead. The first comment here shows how to create a complex type.

编辑

多看了几遍,这里是一个 更好的例子.

After looking around a bit more, here is a much better example.

根据 addComplextType (lib/class.wsdl.php)

There are 2 ways to register a complex type with NuSoap, per the docblock on addComplextType (lib/class.wsdl.php)

/**  
* adds an XML Schema complex type to the WSDL types
*
* @param string $name
* @param string $typeClass (complexType|simpleType|attribute)
* @param string $phpType currently supported are array and struct (php assoc array)
* @param string $compositor (all|sequence|choice)
* @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
* @param array $elements e.g. array ( name => array(name=>'',type=>'') )
* @param array $attrs e.g. array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]'))
* @param string $arrayType as namespace:name (xsd:string)
* @see nusoap_xmlschema
* @access public
*/

在我发布的后面的例子中看看他是如何做到的:

See how he does it on the later example I posted:

$server->wsdl->addComplexType('Contact',
    'complexType',
    'struct',
    'all',
    '',
    array(
            'id' => array('name' => 'id', 'type' => 'xsd:int'),
            'first_name' => array('name' => 'first_name', 'type' => 'xsd:string'),
            'last_name' => array('name' => 'last_name', 'type' => 'xsd:string'),
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'phone_number' => array('name' => 'phone_number', 'type' => 'xsd:string')
    )
);

那么如何返回Contact复杂类型的响应:

Then how to return the response with the Contact complex type:

function updateContact($in_contact) {
    $contact = new Contact($in_contact['id']);
    $contact->first_name=mysql_real_escape_string($in_contact['first_name']);
    $contact->last_name=mysql_real_escape_string($in_contact['last_name']);
    $contact->email=mysql_real_escape_string($in_contact['email']);
    $contact->phone_number=mysql_real_escape_string($in_contact['phone_number']);
    if ($contact->update()) return true;
}

您还可以在他的示例中看到如何使用数组变体.抱歉回复太长了!

You can also see how to use the array variant in his example. Sorry for the huge answer!

这篇关于PHP NuSoap 中的复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆