【问题标题】:PHP - How to set https headers to soap ws requestPHP - 如何将 https 标头设置为肥皂 ws 请求
【发布时间】:2017-12-16 04:21:15
【问题描述】:

我正在使用 SoapUI 5.3.0 来测试 SOAP ws 请求。 我被要求通过 https 标头而不是通过 soap 标头发送用户和密码。 当我使用这个 SoapUi 工具时效果很好:

但是,当我尝试从 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);

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

【问题讨论】:

    标签: php soap https http-headers soapui


    【解决方案1】:

    尝试:

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

    【讨论】:

      【解决方案2】:

      最后我完成了使用 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        相关资源
        最近更新 更多