PHP - 如何将https标头设置为soap ws请求

时间:2017-07-11 23:04:59

标签: php soap https http-headers soapui

我使用SoapUI 5.3.0来测试SOAP ws请求。 我被要求通过https标头发送用户和密码,而不是通过soap标头。 当我使用这个SoapUi工具时,它运行良好:

SoapUI 5.3.0 Capture 但是,当我尝试从php执行此操作时,我总是会收到身份验证错误,当我故意使用错误的密码时,我得到了完全相同的错误,我已经尝试了几种组合,但没有一个给我预期的结果

代码示例:

$data['Contrato'] = '123456';
$data['FechaInicio'] = '11/07/2017';
$data['FechaFin'] ='11/07/2017';

$client = new SoapClient( "https://example.com/WebService?WSDL", array(
    "exceptions" => 0,
    "trace" => 1,
    'stream_context' => stream_context_create(array(
        'http' => array(
            'header' => 'Username:xxx@gmail.com\\n\\r Password:notrealpwd'
        ),
    )),
));

$result = $client->__soapCall('depositos', $data);

你们有谁知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

尝试:

$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
                 "login" => $login, "password" => $password) );

答案 1 :(得分:0)

最后,我使用curl设置了所需的标题,解决了它。

<?php 
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
                    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hub="https://example.com/WebService?WSDL">
                       <soapenv:Header/>
                       <soapenv:Body>
                          <hub:depositos>
                             <!--Optional:-->
                             <hub:solicitud>
                                <Contrato>123456</Contrato>
                                <FechaInicio>11/07/2017</FechaInicio>
                                <!--Optional:-->
                                <FechaFin>11/07/2017</FechaFin>
                             </hub:solicitud>
                          </hub:depositos>
                       </soapenv:Body>
                    </soapenv:Envelope>';

$headers = array(
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "SOAPAction: ''",
    "Content-length: ".strlen($xml_post_string),
    "Username: xxx@gmail.com",
    "Password: notrealpwd"
);

// PHP cURL  for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $ws_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
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);