gpt4 book ai didi

php - 将 xmlns 值添加到 Symfony2 SoapServer 响应

转载 作者:行者123 更新时间:2023-12-01 05:48:21 26 4
gpt4 key购买 nike

我们已经构建了一个 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 服务响应如下:
<?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/"进入上述回复。

管理请求并返回响应的 Controller 如下:
    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');调用 你好.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();

关于php - 将 xmlns 值添加到 Symfony2 SoapServer 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734739/

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