[英]PHP soapclient using proxy with authentication, and custom http header
我正在使用Soap客户端连接到wsdl服务。 肥皂客户端使用自定义身份验证模式,我通过以下代码将它们添加到http
$client=new SoapClient(
$wsdl,
array(
'trace' => 1,
"stream_context" => stream_context_create(
array(
"http"=>array(
"header"=> "username: xmluatbank\r\n".
"password: 123456\r\n"
)
)
)
);
标头如下。
POST /myweb/Proxy HTTP/1.1
Host: uat.myweb.com:8080
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.4.19
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 1086
username: xmluatbank
password: 123456
到目前为止,一切都很好,然后出现了需要通过代理进行连接的新请求,因此我将代码更改为:
$client=new SoapClient(
$wsdl,
array(
'trace' => 1,
"stream_context" => stream_context_create(
array(
"http"=>array(
"header"=> "username: xmluatbank\r\n".
"password: 123456\r\n"
)
),
'proxy_host' => $proxy,
'proxy_port' => $proxyport,
'proxy_login' => $proxyusrname,
'proxy_password' => $proxypwd,
)
);
头变成这样:
POST http://uat.myweb.com:8080/ctos/Proxy HTTP/1.1
Host: uat.myweb.com:8080
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.9-ZS5.6.0
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 1086
Proxy-Authorization: Basic YWdyb2JhbmtcanVyaXMudnRjbDpBYmNkMTIx==
如您所见,我现在拥有的流上下文已消失,所以现在我得到了wsdl服务器的身份验证错误。 看起来代理覆盖了http标头。 所以我尝试将代理添加到http标头,而不使用soap代理
$sLogin = base64_encode("$proxyusername:$proxypwd");
$aHTTP['http']['proxy'] = "$proxy:$proxyprot";
$aHTTP['http']['method'] = 'POST';
$aHTTP['http']['header'] = "User-Agent: My PHP Script\r\n";
$aHTTP['http']['header'] .= "Proxy-Authorization: Basic $sLogin";
$aHTTP['http']['header'] .= "username: xmluatbank\r\n"."password: 1234567\r\n";
$context = stream_context_create($aHTTP);
并这样称呼肥皂客户:
$client=new SoapClient(
$wsdl,
array(
'trace' => 1,
"stream_context" => $context
)
);
但是这种方式我得到肥皂错误,无法连接到代理。
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://uat.myweb.com:8080/ctos/Proxy?wsdl' : failed to load external entity "http://uat.myweb.com:8080/myweb/Proxy?wsdl"
我不知道如何解决这个问题。 我尝试了我所知道的一切,请帮助
我通过以下代码解决了这个问题,这要归功于我们的一位同事,看来问题出在服务器上的旧版本php中
if ( PHP_VERSION <5.4 ) {
echo "old_version";
$str_auth_header = "username: xmluatbank\r\n".
"password: 123456";
ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $str_auth_header);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.