gpt4 book ai didi

php - PHP NuSoap 中的复杂类型

转载 作者:行者123 更新时间:2023-12-04 23:27:18 24 4
gpt4 key购买 nike

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

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

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

在此先感谢您的帮助。

最佳答案

您正在写的内容称为 proxy .

some examples在线用于 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')));

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

另一种选择是考虑原生 SoapServer类代替。第一条评论 here展示了如何创建复杂类型。

编辑

再四处看看后,这里有一个 much better example .

根据 addComplextType (lib/class.wsdl.php) 上的文档 block ,有两种方法可以使用 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')
)
);

然后如何返回具有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;
}

您还可以在他的示例中看到如何使用数组变体。很抱歉回答这么大!

关于php - PHP NuSoap 中的复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20616595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com