zend_soap에서 네임 스페이스를 어떻게 제거합니까?
-
20-09-2019 - |
문제
MyMemory에서 Tranlsation 웹 서비스를 사용하려고합니다. http://mymemory.translated.net/doc/spec.php
불행히도 Zend_soap_client는 서비스에서 인식하지 못하는 XML Reqest 객체를 생성합니다. 나는 그것이 태그 내의 NS1-Attribute (네임 스페이스) 때문이라고 생각합니다. 그렇다면 누구든지 제거하는 방법을 아는 사람이 있습니까?
그것이 기본적으로 내가하는 일입니다.
$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);
}
}
제휴하지 않습니다 StackOverflow