我试图使用从存储器我的tranlsation Web服务: HTTP://mymemory.translated。净/ DOC / spec.php

不幸的是,Zend_Soap_Client确实产生未由服务识别的XML传请求对象。我想这是因为在标签内的NS1属性(命名空间)的。因此,没有人知道如何清理?

这基本上是我做的:

$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 );
    }
}

其他提示

对于一个Zend 2应用程序,你可以添加一个额外的类到你的控制器,在飞行的SOAP请求的替代品。

示例:

class MySoapClient extends Zend\Soap\Client {

     public function _doRequest(Zend\Soap\Client\Common $client, $request, $location, $action, $version, $oneWay = null)
     {
         $bad = '<env:Envelope ...>';
         $good = '<env:Envelope ...>';
         $request = str_replace($bad, $good, $request);
         // Perform request as is
         if ($oneWay === null) {
             return call_user_func(array($client, 'SoapClient::__doRequest'), $request, $location, $action, $version);
         }
         return call_user_func(array($client, 'SoapClient::__doRequest'), $request, $location, $action, $version, $oneWay);
     }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top