我正在使用PHP中的NuSoap库构建一个web服务。我的web服务将充当客户端和供应商已经存在的web服务之间的中间层。因此,他们将连接到我的web服务,而不是客户端直接连接到供应商,我的web service连接到供应商并获取响应并将相同的响应发送回客户端。
我唯一的问题是,我的供应商正在发送回stdclass对象(他们的Web服务是用.Net编写的),而我必须接收该对象,并在我的Web服务方法上将相同的对象发送回客户端。
我在互联网上搜索了很多,但没有明确的方法可以通过NuSoap库做到这一点。到目前为止,无论我读到什么,都要指定我必须使用复杂类型来实现这一点,但我也不知道如何获取响应,然后将其转换为复杂类型并将其发送回客户端。
提前感谢您的帮助。
您正在编写的内容被称为代理。
网上有一些NuSoap服务器通过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')));
实现代理的一种方法是使用存根数据构建服务,这样它就不会首先与后端服务进行通信。看看你是否能让原始客户对你的代理的人为响应感到满意。然后,一旦有了这些,使用真正的后端服务应该是微不足道的(根据我的经验,SOAP客户端操作比服务器操作更容易)。
另一种选择是考虑本机SoapServer类。这里的第一条注释显示了如何创建复杂类型。
编辑
在多看了一眼之后,这里有一个更好的例子。
根据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
*/
看看他是如何做到这一点的,稍后我发布的例子:
$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')
)
);
然后如何使用联系人复杂类型返回响应:
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;
}
您还可以在他的示例中看到如何使用数组变体。抱歉回答太多了!