gpt4 book ai didi

php - 我如何删除 Zend_Soap 中的 namespace ?

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:35 25 4
gpt4 key购买 nike

我正在尝试使用来自 MyMemory 的翻译网络服务:http://mymemory.translated.net/doc/spec.php

不幸的是,Zend_Soap_Client 确实生成了服务无法识别的 XML 请求对象。我想这是因为标签中的 ns1-Attribute (Namespace)。那么有谁知道如何删除它们?

这基本上就是我所做的:

$client = new Zend_Soap_Client('http://mymemory.translated.net/otms/?wsdl', array(
'soap_version' => SOAP_1_1
));

然后我调用函数:

try {
$client->otmsGet(array(
'key' => 'xxx',
'q' => array(
'source' => 'Computer Science',
'source_lang' => 'en-US',
'target_lang' => 'de-DE'
)
));
} catch(Exception $e) {
print $client->getLastRequest();
}

生成的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:otmsGet>
<ns1:key>xxx</ns1:key>
<ns1:q>
<ns1:source>Computer Science</ns1:source>
<ns1:source_lang>en-US</ns1:source_lang>
<ns1:target_lang>de-DE</ns1:target_lang>
</ns1:q>
</ns1:otmsGet>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

它实际上应该是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<otmsGet xmlns="http://tempuri.org/">
<key xmlns:SOAPSDK1="http://tempuri.org/">mmDemo123</key>
<q xmlns:SOAPSDK2="http://tempuri.org/">
<source>control panel</source>
<source_lang>en-US</source_lang>
<target_lang>es-ES</target_lang>
</q>
</otmsGet>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

感谢您的帮助!

最佳答案

我必须创建一个包装器:

class My_Soap_Client_Namespace extends Zend_Soap_Client_Common {
protected $namespace = null;

function __construct($doRequestCallback, $wsdl, $options) {
if (array_key_exists('namespace', $options)) {
$this->namespace = $options['namespace'];
}
parent::__construct($doRequestCallback, $wsdl, $options);
}

function __doRequest($request, $location, $action, $version, $one_way = null) {
if ($this->namespace != null) {
$request = preg_replace ( '/<ns1:(\w+)/', '<$1 xmlns="' . $this->namespace . '"', $request, 1 );
$request = preg_replace ( '/<ns1:(\w+)/', '<$1', $request );
$request = str_replace ( array ('/ns1:', 'xmlns:ns1="' . $this->namespace . '"' ), array ('/', '' ), $request );
}

return parent::__doRequest ( $request, $location, $action, $version );
}
}

关于php - 我如何删除 Zend_Soap 中的 namespace ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2464466/

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