通过带有证书的PHP / Soap / WSDL发送XML

时间:2019-03-26 15:15:17

标签: php xml soap wsdl

我对Soap完全不了解,我需要使用此链接a link使用证书连接到soap / wsdl Web服务,发送XML文件并获取响应。 XML由表单生成并上传到服务器。

我在这里找到了示例,但我不知道如何使用它(下面的代码)。 我知道我必须使用“ pem”文件而不是“ pfx”,并且已经按照其他示例进行了转换。 我已经生成了XML(下面的文件)。 在服务器上,PHP / Soap Client的设置也可以。 我在php 7.2中使用PHP LAMP。

肥皂的例子:

function Curl_Soap_Request($request, $url)
{
    /**
     * @param request is your xml for soap request 
     * @param url is location of soap where your request with hit
     */

    $keyFile = getcwd() . "\\privatekey.pem"; //
    $caFile = getcwd() . "\\certificate.pem"; //
    $certPass = "test123";
    // xml post structure

    $xml_post_string = $request; // data from the form, e.g. some ID number

    $headers = array(
    "Content-type:  application/soap+xml; charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    // "SOAPAction: '/Imp1/ApplicantEligibilityService",
    "Content-length: " . strlen($xml_post_string),
    ); //SOAPAction: your op URL

    //$url = $soapUrl;

    // PHP cURL  for https connection with auth
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    // The --key option - If your key file has a password, you will need to set
    // this with CURLOPT_SSLKEYPASSWD
    //  curl_setopt($ch, CURLOPT_SSLKEY, $keyFile);
    curl_setopt($ch, CURLOPT_SSLKEY,  $keyFile);

    // The --cacert option
    curl_setopt($ch, CURLOPT_SSLCERT,  $caFile);

    // The --cert option
    //curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //   curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_TIMEOUT, 180);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // converting
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;

} 

我需要发送的XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.solicitacao.selodigital.tjce.jus.br/" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
<soapenv:Header/>
<soapenv:Body>
<ser:solicitaSelos>
<arg0>
<cabecalho>
<versao>1.12</versao>
<dataHora>"2019-03-21T15:28:48.147306-03:00"</dataHora>
<ambiente>2</ambiente>
<serventia>
<codigoServentia>000321</codigoServentia>
</serventia>
</cabecalho>
<solicitante>
<nomePessoa></nomePessoa>
<documento>
<tipoDocumento>1</tipoDocumento>
</documento>
</solicitante>
<idSolicitacaoselo>4</idSolicitacaoselo>
<itens>
<itemSolicitacao>
<sequencial>1</sequencial>
<codigoSelo>
<codigo>14</codigo>
</codigoSelo>
<quantidade>1500</quantidade>
<sequencial>2</sequencial>
<codigoSelo>
<codigo>76</codigo>
</codigoSelo>
<quantidade>5000</quantidade>
</itemSolicitacao>
<itens>
</arg0>
</ser:solicitaSelos>
</soapenv:Body>
</soapenv:Envelope>

0 个答案:

没有答案