PHP consume soap service using Basic auth

我是研究僧i 提交于 2020-12-06 12:36:47

问题


I have a server that connecting to it using CURL is only possible using

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

Now i need to connect to the same server using SOAP How is it possible to set basic authentication to soapclient ?

Thanks


回答1:


php-soap has a limitation/bug, which does not allow to retrieve the WSDL while using Authentication (bug report)

You can update/upgrade your PHP version. There are people reporting that this bug is solved on 5.4.8 version.

To keep php-soap you must use a local/static version of the WSDL what I DO NOT recommend.

Although it could be less performant, I suggest you to have a look at another PHP Soap Client library, like NuSOAP.




回答2:


We were having a similar issue with a JDE connection. The authentication entity WSSE needed to be added along with the wsdl server accepting requests from your server (where the php script lives)

$Username = '******';
$Password = '******';
$xmlHeader =
                '<soapenv:Header> 
                        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soapenv:mustUnderstand="1">
                        <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                                <wsse:Username>'.$Username.'</wsse:Username>   
                                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$Password.'</wsse:Password>  
                        </wsse:UsernameToken>
                        </wsse:Security>
                </soapenv:Header>';

Some extra envelope information should be set also

$this->ch = curl_init( $this->postURL );
                curl_setopt( $this->ch, CURLOPT_POST, TRUE);
                curl_setopt( $this->ch, CURLOPT_SSLVERSION, 'SSLv3');
                curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $this->postArg);
                curl_setopt( $this->ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt( $this->ch, CURLOPT_HEADER, 0);
                curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt( $this->ch, CURLOPT_VERBOSE, TRUE);
                curl_setopt( $this->ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));

                // GET RESPONSE AND RESPONSE CODE
                try{
                        $this->response = curl_exec( $this->ch );
                        $this->responseCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);

                        if($this->responseCode != '200'){
                                //ERROR!!
                        }
                }catch(Exception $e){
                        $e->getMessage();
                }

Pay attention to the CURLOPT_SSLVERSION = SSLv3. You will need to see what version SSL you may need to get access to your WSDL.




回答3:


Did you try to use 'login' and 'password' from SoapClient constructor options? They are described here: http://pl1.php.net/manual/en/soapclient.soapclient.php




回答4:


You need to send a soap header with the authentication info :

$param = new SoapVar(array('username' => 'username', 'password' => 'password'), SO AP_ENC_OBJECT);
$header = new SoapHeader('http://localhost/soap_server/server.php', 'AuthenticationInfo', $param, false);

And then pass it to the SOAP client object

$client = new SoapClient($url, array('trace' => 1));
$client->__setSoapHeaders(array($header));



回答5:


Actually it is possible with standart methods of php-soap.

There is an option called stream_context. More info on this here: http://php.net/manual/en/context.php

You should provide a context with Authorization header. Example:

$context = [
    'ssl'  => [
        'verify_peer'      => false, 
        'verify_peer_name' => false,
    ],
    'http' => [
        'header' => 'Authorization: Basic ' .base64_encode('user:pass'),
    ],
];
$client = new \SoapClient('wdsl', [
    'login'          => 'user',
    'password'       => 'pass',
    'stream_context' => stream_context_create($context),
]);


来源:https://stackoverflow.com/questions/24928336/php-consume-soap-service-using-basic-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!