代码之家  ›  专栏  ›  技术社区  ›  Josafat

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

  •  1
  • Josafat  · 技术社区  · 8 年前

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

    SoapUI 5.3.0 Capture

    代码示例:

    $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);
    

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

    2 回复  |  直到 8 年前
        1
  •  0
  •   Rao CrashOverload    8 年前

    $client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
                     "login" => $login, "password" => $password) );
    
        2
  •  0
  •   Josafat    7 年前

    最后,我使用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);