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条)

报告相同问题?

悬赏问题

  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值