简体   繁体   中英

NTLM authentication + soap client + response not received

I have the following code to call the NTLM authentication for Sharepoint data:

class NTLMSoapClient extends SoapClient {
/**
 * Whether or not to validate ssl certificates
 *
 * @var boolean
 */

    function __construct($location, $user, $passwd)
    {
        //print '1';

        $headers = array(

            'Method: POST',

            'Connection: Keep-Alive',

            'User-Agent: PHP-SOAP-CURL',

            'Content-Type: text/xml; charset=utf-8',

            'SOAPAction: ""',
        );

        $request='';

        //$this->__last_request_headers = $headers;

        $ch = curl_init($location);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_HTTPHEADER , $headers);
        //print '2';

        curl_setopt($ch, CURLOPT_POST , true );

        curl_setopt($ch, CURLOPT_POSTFIELDS , $request);

        curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);

        // the next option may or may not cause issues with non-XML documents
        //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);

        curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$passwd);
        //print '3';

        // optional SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $response = curl_exec($ch);
        print $response;

        // TODO: Add some real error handling.
        // If the response if false than there was an error and we should throw
        // an exception.
        if ($response === false) {
            //print '5';
            throw new EWS_Exception(
              'Curl error: ' . curl_error($ch),
              curl_errno($ch)
            );
        }

        //print '6';

        return $response;
    }
}

The call is made as follows:

try{
            $this->soapObject = new NTLMSoapClient( $this->wsdl, $this->spUser, $this->spPass);
        }catch(SoapFault $fault){
            //If we are unable to create a Soap Client display a Fatal error.
            throw new Exception("Unable to locate WSDL file.");
        }

The response is coming as wsdl. Why is the response coming as wsdl. It should return me the soap client object array as in normal soap client call.

Here is a very simplified version of your example code:

class NTLMSoapClient extends SoapClient {

    function __construct($location, $user, $passwd) {
        $ch = curl_init($location);
        $response = curl_exec($ch);
        print $response;
        return $response; 
    }
}

$this->soapObject = new NTLMSoapClient( $this->wsdl, $this->spUser, $this->spPass);

Notice the following:

  1. Like the parent SoapClient , you pass a wsdl url as the first parameter.
  2. In your constructor, you map the wsdl url to the parameter $location .
  3. In your curl_init , you pass in $location (which is the wsdl url passed in when instantiating the object).
  4. You store the curl_exec() response as $response
  5. Ignoring the print $resposne; with the assumption that this is meant for troubleshooting, your final action for the object constructor is to return the $response variable to the variable that instantiated the NTLMSoapClient class.

With all of the above in mind...

a. Your class is acting more as a function in that creating the object simply results in getting the curl response back (the object effectively is no longer an object, but a string containing the response). At best this could be considered a sort of "static constructor".

b. The variable, having been set to the returned string couldn't call a method of the NTLMSoapClient class later, because it's just a string.

c. You always pass in the wsdl url, so the response will always be what that url returns, ie the wsdl you requested via the curl request.

d. I don't think that constructors are supposed to return anything, other than an object of the instantiated class.

e. the native SoapClient doesn't return anything, so you should try to model your extended class more closely to the parent class.

f. Even if this did work (meaning either there is something about OOP and PHP I don't understand that's involved), your example only shows the NTLMSoapClient requesting the initial wsdl, without any follow-up sample code showing it calling a method of the web service or anything else, so where in your example code are you expecting anything other than the wsdl?

g. Finally, to end on a positive note that I hope encourages you to keep working toward your overall goal, the one thing you know for sure is that the extended class returns the wsdl, which means that you are successfully authenticating to the service server via NTLM, which is the first half of your objective, if I'm understanding the idea of the code and your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM