简体   繁体   English

PHP NuSoap中的复杂类型

[英]Complex type in PHP NuSoap

I am building a web service using NuSoap library in PHP. 我正在使用PHP中的NuSoap库构建Web服务。 My webservice will act as a middle layer between client and an already existing web service by a vendor. 我的Web服务将充当客户端和供应商已经存在的Web服务之间的中间层。 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. 因此,他们将连接到我的Web服务,而不是客户端直接连接到供应商,我的Web服务将连接到供应商并获取响应并将相同的响应发送回客户端。

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. 我唯一的问题是我的供应商正在发送回stdclass对象(他们的Web服务是用.Net编写的),我必须接收该对象并将相同的对象发送回我的Webservice方法上的客户端。

I have searched quite a bit on Internet but there are no clear ways how to do acheive this by NuSoap library. 我已经在Internet上进行了很多搜索,但是NuSoap库没有明确的方法来实现这一目标。 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. 到目前为止,无论我读过什么,都必须指定必须使用复杂类型来实现此目的,但是我仍然不知道如何获取响应,然后将其转换为复杂类型并将其发送回客户端。

Thanks in advance for your help. 在此先感谢您的帮助。

What you are writing is referred to as a proxy . 您所写的内容称为代理

There are some examples online for NuSoap server sending complex types through the addComplexType method. 在线上有一些有关NuSoap服务器通过addComplexType方法发送复杂类型的addComplexType

//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')));

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). 然后,一旦有了这些,使用真正的后端服务就变得微不足道了(根据我的经验,SOAP客户端操作比服务器操作更容易)。

Another alternative is to consider the native SoapServer class instead. 另一种选择是考虑使用本机SoapServer类。 The first comment here shows how to create a complex type. 这里的第一个注释显示了如何创建复杂类型。

EDIT 编辑

After looking around a bit more, here is a much better example . 环顾四周后,这里有一个更好的例子

There are 2 ways to register a complex type with NuSoap, per the docblock on addComplextType (lib/class.wsdl.php) addComplextType (lib / class.wsdl.php)上的每个文档块中,有两种方法可以使用NuSoap注册复杂类型。

/**  
* 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')
    )
);

Then how to return the response with the Contact complex type: 然后如何返回Contact复杂类型的响应:

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! 对不起,答案很大!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM