PHP soapclient使用代理与身份验证,以及自定义http标头

时间:2014-09-25 09:58:35

标签: php soap proxy wsdl soap-client

我使用soap客户端连接到wsdl服务。 soap客户端使用自定义身份验证模式,我通过以下代码将其添加到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"

我不知道如何解决这个问题。我尝试了所有我认识的,请帮助

1 个答案:

答案 0 :(得分:1)

我通过以下代码解决了这个问题,感谢我们的一位同事,看起来问题是服务器上的旧版本的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);
}