douyan6742 2017-07-11 23:04
浏览 328
已采纳

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

I'm using SoapUI 5.3.0 to test a SOAP ws request. I was requested to send user and password via https headers not via soap headers. It works well when I use this SoapUi tool:

SoapUI 5.3.0 Capture However, when I try to do it from php I always get an authentication error, exactly the same error I get when I use a wrong password on purpose, I've tried several combinations but none of them gave me expected results

code example:

$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\
\ Password:notrealpwd'
        ),
    )),
));

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

Does any of you knows what I am doing wrong?

  • 写回答

2条回答 默认 最新

  • doujiang2643 2018-04-11 23:40
    关注

    At the end I finished using curl to set the needed headers, that solved it.

    <?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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波