Getting blank response from exchange server (RE:Talking SOAP With Exchange)

Discussion in 'HOWTO-Related Questions' started by lightsabersetc, Jul 10, 2011.

  1. lightsabersetc

    lightsabersetc New Member

    Hello,

    I just followed the tutorial at http://www.howtoforge.com/talking-soap-with-exchange. I am getting a successful response from $client->__getFunctions() & $client->__getTypes(), but am receiving a blank response on any call thereafter. Below is my code and some relevant output:

    Code:
    class NTLMSoapClient extends SoapClient {
    		function __doRequest($request, $location, $action, $version) {
    			$headers = array(
    				'Method: POST',
    				'Connection: Keep-Alive',
    				'User-Agent: PHP-SOAP-CURL',
    				'Content-Type: text/xml; charset=utf-8',
    				'SOAPAction: "'.$action.'"',
    			);  
    			$this->__last_request_headers = $headers;
    			$ch = curl_init($location);
    			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    			curl_setopt($ch, CURLOPT_POST, true );
    			curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    			curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
    			curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->password);
    			
    			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    			$response = curl_exec($ch);
    			
    			echo $request . "<BR>" . $response;
    			
    			return $response;
    		}   
    		function __getLastRequestHeaders() {
    			return implode("n", $this->__last_request_headers)."n";
    		}   
    	}
    Code:
    class ExchangeNTLMSoapClient extends NTLMSoapClient {
    		protected $user = 'me@exchange.com';
    		protected $password = 'password';
    	}
    Code:
    class NTLMStream {
    		private $path;
    		private $mode;
    		private $options;
    		private $opened_path;
    		private $buffer;
    		private $pos;
    
    		public function stream_open($path, $mode, $options, $opened_path) {
    			echo "[NTLMStream::stream_open] $path , mode=$mode n";
    			$this->path = $path;
    			$this->mode = $mode;
    			$this->options = $options;
    			$this->opened_path = $opened_path;
    			$this->createBuffer($path);
    			return true;
    		}
    
    		public function stream_close() {
    			echo "[NTLMStream::stream_close] n";
    			curl_close($this->ch);
    		}
    
    		public function stream_read($count) {
    			echo "[NTLMStream::stream_read] $count n";
    			if(strlen($this->buffer) == 0) {
    				return false;
    			}
    			$read = substr($this->buffer,$this->pos, $count);
    			$this->pos += $count;
    			return $read;
    		}
    
    		public function stream_write($data) {
    			echo "[NTLMStream::stream_write] n";
    			if(strlen($this->buffer) == 0) {
    				return false;
    			}
    			return true;
    		}
    
    		public function stream_eof() {
    			echo "[NTLMStream::stream_eof] ";
    			if($this->pos > strlen($this->buffer)) {
    				echo "true n";
    				return true;
    			}
    			echo "false n";
    			return false;
    		}
    
    		/* return the position of the current read pointer */
    		public function stream_tell() {
    			echo "[NTLMStream::stream_tell] n";
    			return $this->pos;
    		}
    
    		public function stream_flush() {
    			echo "[NTLMStream::stream_flush] n";
    			$this->buffer = null;
    			$this->pos = null;
    		}
    
    		public function stream_stat() {
    			echo "[NTLMStream::stream_stat] n";
    			$this->createBuffer($this->path);
    			$stat = array(
    				'size' => strlen($this->buffer),
    			);
    			return $stat;
    		}
    
    		public function url_stat($path, $flags) {
    			echo "[NTLMStream::url_stat] n";
    			$this->createBuffer($path);
    			$stat = array(
    				'size' => strlen($this->buffer),
    			);
    			return $stat;
    		}
    
    		/* Create the buffer by requesting the url through cURL */
    		private function createBuffer($path) {
    			if($this->buffer) {
    				return;
    			}
    			echo "[NTLMStream::createBuffer] create buffer from : $pathn";
    			$this->ch = curl_init($path);
    			curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    			curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
    			curl_setopt($this->ch, CURLOPT_USERPWD, $this->user.':'.$this->password);
                            
                            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    			echo $this->buffer = curl_exec($this->ch);
    			echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytesn";
    			$this->pos = 0;
    		}
    	}
    Code:
    class ExchangeNTLMStream extends NTLMStream {
    		protected $user = 'me@exchange.com';
    		protected $password = 'password';
    	}
    index.php
    Code:
    $wsdl = dirname(__FILE__)."\\Services.wsdl";
        
        stream_wrapper_unregister('https');
        stream_wrapper_register('https', 'ExchangeNTLMStream') or die("Failed to register protocol");
        
        $client = new ExchangeNTLMSoapClient($wsdl);
                
        /* Do something with the web service connection */
        stream_wrapper_restore('https');
        
        var_dump($client->__getFunctions());
        //var_dump($client->__getTypes());
        $FindFolder->Traversal = "Shallow";
        $FindFolder->FolderShape->BaseShape = "Default";
        $FindFolder->ParentFolderIds->DistinguishedFolderId->Id = "root";
        $result = $client->FindFolder($FindFolder);
        var_dump($result);
        $folders = $result->ResponseMessages->FindFolderResponseMessage->RootFolder->Folders->Folder;
        foreach($folders as $folder) {
                echo $folder->DisplayName."n";
        }
    The var_dump of $result returns NULL. I dumped $request and $response in the class NTLMSoapClient, which yielded 'Default' and 'NULL' respectively. I have tried basically every function with the same results. At this point, I've been banging my head against the table for quite a while trying to get this to work so I'm hoping one of you can help :)

    Thanks in advance!
     
  2. arune

    arune New Member

    Im having the same problem, I think.
    I used this tutorial in 2009 and just a few month ago the script stopped working, after reading this comment I tested my script on an older Ubuntu release and it was working without problems there.
    Non working php version: 5.3.2 (also tested on 5.3.3)
    Working php version: 5.2.4

    Any hint on how to solve this would be nice.
     
  3. arune

    arune New Member

    I just made a very simple script which logs in to an exchange server and fetches Services.wsdl:

    Code:
    <?php
    $ch = curl_init(); 
    
    curl_setopt($ch, CURLOPT_URL, "https://server.com/EWS/Services.wsdl");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
    curl_setopt($ch, CURLOPT_USERPWD,"username:password");
    
    $result = curl_exec($ch);
    
    curl_close($ch);
    
    echo "datalength: ".strlen($result);
    ?>
    
    I guess php-curl could be blaimed?
    Non working php-curl version: 5.3.2 (also tested on 5.3.3)
    Working php-curl version: 5.2.4
     
  4. arune

    arune New Member

    Yes, after investigation I found this bug report.
    "On a fully updated version of Ubuntu 10.10 amd64, under both curl and php5-curl, we were unable to use NTLM authentication to talk to our exchange server. Regressing libcurl3 from 7.21.0 to 7.19.5 from karmic fixed the problem for both applications."
     

Share This Page