PHP - 如何将 https headers 设置为 soap ws 请求
PHP - How to set https headers to soap ws request
我正在使用 SoapUI 5.3.0 来测试 SOAP ws 请求。
我被要求通过 https headers 而不是通过 soap headers 发送用户名和密码。
当我使用这个 SoapUi 工具时效果很好:
但是,当我尝试从 php 执行此操作时,我总是遇到身份验证错误,这与我故意使用错误密码时遇到的错误完全相同,我尝试了几种组合,但其中 none给了我预期的结果
代码示例:
$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);
你们有谁知道我做错了什么吗?
试试:
$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
"login" => $login, "password" => $password) );
最后我完成了使用 curl 设置所需的 headers,解决了它。
<?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);
我正在使用 SoapUI 5.3.0 来测试 SOAP ws 请求。 我被要求通过 https headers 而不是通过 soap headers 发送用户名和密码。 当我使用这个 SoapUi 工具时效果很好:
代码示例:
$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);
你们有谁知道我做错了什么吗?
试试:
$client = new SoapClient($wsdl, array("trace" => 1, "exceptions" => 0,
"login" => $login, "password" => $password) );
最后我完成了使用 curl 设置所需的 headers,解决了它。
<?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);