使用PHP SoapClient调用Sharepoint WebService

时间:2014-10-14 12:12:36

标签: php web-services sharepoint soap soap-client

我扩展了PHP SoapClient以将其与NTLM Sharepoint身份验证一起使用:

    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_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        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);
        $response = curl_exec($ch);
        return $response;
    }

    function __getLastRequestHeaders() {
        return implode("n", $this->__last_request_headers) . "n";
    }

    public final function __call($methodName, array $methodParams) {
        /*
         * Is soapClient set? This check may look double here but in later
         * developments it might help to trace bugs better and it avoids calls
         * on wrong classes if $soapClient got set to something not SoapClient.
         */
        if (!$this->soapClient instanceof \SoapClient) {
// Is not set
            throw new \Exception('Variable soapClient is not a SoapClient class, have: ' . gettype($this->soapClient), 0xFF);
        }

// Is it a "SOAP callback"?
        if (substr($methodName, 0, 2) == '__') {
// Is SoapClient's method
            $returned = call_user_func_array(array($this->soapClient, $methodName), $methodParams);
        } else {
// Call it
            $returned = $this->soapClient->__call($methodName, $methodParams);
        }

// Return any values
        return $returned;
    }

}

class SPNTLMSoapClient extends NTLMSoapClient {

    protected $user = 'xxxxxx';
    protected $password = 'xxxxxxx';

}

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);
        echo $this->buffer = curl_exec($this->ch);
        echo "[NTLMStream::createBuffer] buffer size : " . strlen($this->buffer) . "bytesn";
        $this->pos = 0;
    }

}

class SPNTLMStream extends NTLMStream {

    protected $user = 'xxxxxxx';
    protected $password = 'xxxxxxxx';

}



stream_wrapper_unregister('https');
stream_wrapper_register('https', 'SPNTLMStream') or die("Failed to register protocol");
$wsdl = "https://wxxxxxxxxx?WSDL";
$client = new SPNTLMSoapClient($wsdl);

我成功地致电:“print_r($ client-> __ getFunctions())”并获得以下内容:

Array
(
    [0] => CopyIntoItemsLocalResponse CopyIntoItemsLocal(CopyIntoItemsLocal $parameters)
    [1] => CopyIntoItemsResponse CopyIntoItems(CopyIntoItems $parameters)
    [2] => GetItemResponse GetItem(GetItem $parameters)
    [3] => CopyIntoItemsLocalResponse CopyIntoItemsLocal(CopyIntoItemsLocal $parameters)
    [4] => CopyIntoItemsResponse CopyIntoItems(CopyIntoItems $parameters)
    [5] => GetItemResponse GetItem(GetItem $parameters)
)

我现在的问题是:如何使用$ client和parameters调用这些方法,

$client->__call('CopyIntoItems', $params)无效

$client->CopyIntoItems($params)无效

我从来没有得到任何回应......

提前......

0 个答案:

没有答案