在PHP中使用文字xml的SOAP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在PHP中使用文字xml的SOAP相关的知识,希望对你有一定的参考价值。
如何使用SoapHeader和__setSoapHeaders通过SOAP创建此特定的xml输出?我不能像我想的那样做同样的XML。我不希望在信封标签中使用ns1和ns2,而在标头中我需要这个Action SOAP-ENV:mustUnderstand =“1”......
这是我的代码:
try{
$client = new SoapClient('https://thesite.com.br/wcf/SvcContratos.svc?wsdl', array('trace' => 1,'use' => SOAP_LITERAL));
$usuario='user_1';
$senha='1234';
$tipo='1';
$header = new SoapHeader("http://schemas.microsoft.com/ws/2005/05/addressing/none","Action", "http://tempuri.org/ISvcContratos/GerarToken");
$client->__setSoapHeaders($header);
$params = new SoapVar("<objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:DsSenha>".$senha."</d4p1:DsSenha><d4p1:DsUsuario>".$usuario."</d4p1:DsUsuario><d4p1:IdTipoConsulta>".$tipo."</d4p1:IdTipoConsulta></objLogin>", XSD_ANYXML);
$data = $client->GerarToken($params);
echo $client->__getLastRequest();
}catch(SoapFault $fault){
echo $client->__getLastRequest()."<br>".$fault->getMessage();
}
有了这个php代码我有这个错误的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/" xmlns:ns2="http://schemas.microsoft.com/ws/2005/05/addressing/none">
<SOAP-ENV:Header>
<ns2:Action>http://tempuri.org/ISvcContratos/GerarToken</ns2:Action>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<GerarToken>
<objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<d4p1:DsSenha>1234</d4p1:DsSenha>
<d4p1:DsUsuario>user_1</d4p1:DsUsuario>
<d4p1:IdTipoConsulta>1</d4p1:IdTipoConsulta>
</objLogin>
</GerarToken>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我需要通过soap发送这个XML:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ISvcContratos/GerarToken</Action>
</s:Header>
<s:Body>
<GerarToken xmlns="http://tempuri.org/">
<objLogin xmlns:d4p1="http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:DsSenha>1234</d4p1:DsSenha>
<d4p1:DsUsuario>USER_1</d4p1:DsUsuario>
<d4p1:IdTipoConsulta>1</d4p1:IdTipoConsulta>
</objLogin>
</GerarToken>
</s:Body>
</s:Envelope>
答案
好吧,最后我从服务器得到了一个肯定的答案,现在我打开了大门,尝试使用wsdl,下面是我用来解决问题的代码:
try{
$client = new SoapClient('https://thesite.com.br/wcf/SvcContratos.svc?wsdl', array('trace' => 1,'use' => SOAP_LITERAL, 'style' => SOAP_DOCUMENT,));
$usuario='user_1';
$senha='1234';
$params = new SoapVar("<GerarToken xmlns='http://tempuri.org/'><objLogin xmlns:d4p1='http://schemas.datacontract.org/2004/07/EPFWeb.Repository.Default.WCF' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><d4p1:DsUsuario>".$usuario."</d4p1:DsUsuario><d4p1:DsSenha>".$senha."</d4p1:DsSenha><d4p1:IdTipoConsulta>Data</d4p1:IdTipoConsulta></objLogin></GerarToken>", XSD_ANYXML);
$data = $client->GerarToken($params);
$xml = json_decode(json_encode($data),true);
print_r($xml);
echo $client->__getLastRequest();
}catch(SoapFault $fault){
echo $client->__getLastRequest()."<br>".$fault->getMessage();
}
以上是关于在PHP中使用文字xml的SOAP的主要内容,如果未能解决你的问题,请参考以下文章