我们已经构建了一个SOAP服务来接收请求,但是给定的响应需要包括一个附加值,我们不知道如何添加它。
我们需要在其中添加xmlns="http://some-url.co.uk/"
:<SOAP-ENV:PrintLocationResponse>
以便其读为<SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">
SOAP服务收到此请求:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<PrintLocation xmlns="http://some-url.co.uk/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<alertMsg>
[ some message goes here]
</alertMsg>
</PrintLocation>
</s:Body>
</s:Envelope>
SOAP Service对此回应:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:PrintLocationResponse>
<PrintLocationResult>
Message contents goes here
</PrintLocationResult>
</SOAP-ENV:PrintLocationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我们需要在响应的这一部分添加xmlns="http://some-url.co.uk/"
:<SOAP-ENV:PrintLocationResponse>
,以便响应如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">
<PrintLocationResult>
Message contents goes here
</PrintLocationResult>
</SOAP-ENV:PrintLocationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我们不知道如何将xmlns="http://some-url.co.uk/"
添加到上述响应中。
管理请求并返回响应的控制器如下:
public function indexAction(Request $request)
{
$soapServer = new \SoapServer('wsdl/hello.wsdl');
$soapServer->setObject($this->get('hello_service'));
$response = new Response();
$response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
ob_start();
$soapServer->handle();
return $response;
}
此$soapServer = new \SoapServer('wsdl/hello.wsdl');
调用hello.wsdl,如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<wsdl:definitions
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
>
<wsdl:message name="helloIn">
<part name="alertMsg" type="xsd:string" />
</wsdl:message>
<wsdl:message name="helloOut">
<part name="PrintLocationResult" type="xsd:string" />
</wsdl:message>
<wsdl:portType name="hellowsdlPortType">
<wsdl:operation name="PrintLocation">
<wsdl:input message="tns:helloIn"/>
<wsdl:output message="tns:helloOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="PrintLocation">
<soap:operation soapAction="http://some-url.co.uk/PrintLocation" style="rpc"/>
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="hellowsdl">
<wsdl:port name="hellowsdlPort" binding="tns:hellowsdlBinding">
<soap:address location="http://backoffice.system/soap" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
上面的WSDL对我们来说是个谜。我们修改了以下示例:https://symfony.com/doc/2.7/controller/soap_web_service.html
此$soapServer->setObject($this->get('hello_service'));
调用HelloService.php,如下所示:
class HelloService
{
/**
* @var EntityManager
*/
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function PrintLocation($msg)
{
$log = new TempIressMsgThree($msg);
$xml = simplexml_load_string($log->getMsg());
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$xml='<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.origoservices.com">
<m_control>
Response message contents goes here.
</m_control>
</message>';
return $xml;
}
有人可以帮忙吗?
为澄清起见,我们需要在其中添加xmlns="http://some-url.co.uk/"
:<SOAP-ENV:PrintLocationResponse>
使其读为<SOAP-ENV:PrintLocationResponse xmlns="http://some-url.co.uk/">
header("Content-type: text/xml");
$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:PrintLocationResponse>
<PrintLocationResult>
Message contents goes here
</PrintLocationResult>
</SOAP-ENV:PrintLocationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
$content = simplexml_load_string($string,'SimpleXMLElement',LIBXML_NONET);
foreach($content->getDocNamespaces(TRUE) as $shortcut=>$namespace){
$content->registerXPathNamespace($shortcut,$namespace );
}
$PLR = $content[0]->xpath('//SOAP-ENV:PrintLocationResponse');
foreach($PLR as $response){
$response->addAttribute('xmlns', 'http://some-url.co.uk/');
}
echo $content[0]->asXML();