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

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?

图片转代码服务由CSDN问答提供 功能建议

我正在使用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'=>'用户名:  xxx@gmail.com \
 \
密码:notrealpwd'
),
)),
)); 
 
 $ result = $ client-> __ soapCall('depositos',$ data)  ; 
   
 
 

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    点赞 评论
  • dongmu1989 2017-07-12 00:11

    try with:

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

相关推荐 更多相似问题