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

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包