soap 类 支持https 支持cookie登录

改良后的

		$client = new SoapClientAuth('wsdl.txt', array(
            'location' => 'https://vpn.shanghai-cis.com.cn/+CSCO+00756767633A2F2F6A6A6A2E61737066677266672E70627A3A38303830++/webservice/batchcredit?wsdl', // 设置server路径
            'uri' => 'https://aa/batchcredit?wsdl',
            'login' => 'b', // HTTP auth login
            'password' => 'a', // HTTP auth password
            'trace'=>true,
            'targetNamespace'=>'http://webservice.creditreport.p2p.sino.com/',
            'cookies'=>array('webvpn='.$cookiestr)
        ));

<?php
/**
 *    SoapClientAuth for accessing Web Services protected by HTTP authentication
 *    Author: tc
 *    Last Modified: 04/08/2011
 *    Update: 14/03/2012 - Fixed issue with CURLAUTH_ANY not authenticating to NTLM servers
 *    Download from: http://tcsoftware.net/blog/
 *
 *    Copyright (C) 2011  tc software (http://tcsoftware.net)
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */




    /**
     * SoapClientAuth
     * The interface and operation of this class is identical to the PHP SoapClient class (http://php.net/manual/en/class.soapclient.php)
     * except this class will perform HTTP authentication for both SOAP messages and while downloading WSDL over HTTP and HTTPS.
     * Provide the options login and password in the options array of the constructor.
     *
     * @author tc
     * @copyright Copyright (C) 2011 tc software
     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     * @link http://php.net/manual/en/class.soapclient.php
     * @link http://tcsoftware.net/
     */
    class SoapClientAuth extends SoapClient{
        public $Username = NULL;
        public $Password = NULL;
        public $Options;
        /**
         *
         * @param string $wsdl
         * @param array $options 注意cookies 是数组,会自动添加上cookie
         */
        function SoapClientAuth($wsdl, $options = NULL)
            {
                @stream_wrapper_unregister('https');
                @stream_wrapper_unregister('http');
                stream_wrapper_register('https', 'streamWrapperHttpAuth');
                stream_wrapper_register('http', 'streamWrapperHttpAuth');
                $this->Options = $options;
                if($options)
                {
                    $this->Username = $options['login'];
                    streamWrapperHttpAuth::$Username = $this->Username;
                    $this->Password = $options['password'];
                    streamWrapperHttpAuth::$Password = $this->Password;
                }


                parent::SoapClient($wsdl, ($options?$options:array()));


                @stream_wrapper_restore('https');
                @stream_wrapper_restore('http');
            }


        function __doRequest($request, $location, $action, $version) {


            $headers = array(
                'User-Agent: PHP-SOAP',
                'Content-Type: text/xml; charset=utf-8',
                'SOAPAction: "' . $action . '"',
                'Content-Length: ' . strlen($request),
                'Expect: 100-continue',
                'Connection: Keep-Alive'
            );


            $this->__last_request_headers = $headers;
            $ch = curl_init($location);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


            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_FAILONERROR, FALSE);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);


            curl_setopt($ch, CURLOPT_USERPWD, $this->Username . ':' . $this->Password);
            if($this->Options) {
                $cookies = $this->Options['cookies'];
                //var_dump($this->Options['cookies']);
                //exit();
                if (is_array($this->Options['cookies'])) {
                    foreach ($this->Options['cookies'] as $cookiesval) {
                        curl_setopt($ch, CURLOPT_COOKIE, $cookiesval);
                    }


                }


            }


            //curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);


            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
            curl_setopt($ch, CURLOPT_CERTINFO, TRUE);


            $response = curl_exec($ch);


            if(($info = curl_getinfo($ch)) && $info['http_code']==200)
                return $response;
            else if($info['http_code']==401)
                throw new Exception ('Access Denied', 401);
            else if(curl_errno($ch)!=0)
            {
                throw new Exception(curl_error($ch), curl_errno($ch));
            }else
                print_r($info);
                echo $info['http_code'];
                throw new Exception('Error', $info['http_code']);
        }
    }


    class streamWrapperHttpAuth
    {
        public static $Username = NULL;
        public static $Password = NULL;


        private $path = NULL;
        private $position = 0;
        private $buffer = NULL;
        private $curlHandle = NULL;


        public function stream_close()
        {
            if($this->curlHandle)
                curl_close ($this->curlHandle);
        }


        public function stream_open($path, $mode, $options, &$opened_path)
        {
            $this->path = $path;
            $response = $this->postRequest($this->path);
            $this->buffer = ($response!==FALSE?$response:NULL);
            $this->position = 0;
            return $response!==FALSE;
        }


        public function stream_eof()
        {
            return $this->position>strlen($this->buffer);
        }


        public function stream_flush()
        {
            $this->position = 0;
            $this->buffer = NULL;
        }


        public function stream_read($count)
        {
            if($this->buffer)
            {
                $data = substr($this->buffer, $this->position, $count);
                $this->position += $count;
                return $data;
            }
            return FALSE;
        }


        public function stream_write($data)
        {
            return ($this->buffer?TRUE:FALSE);
        }


        public function stream_seek($offset, $whence = SEEK_SET)
        {
            switch($whence)
            {
                case SEEK_SET:
                    $this->position = $offset;
                    break;
                case SEEK_CUR:
                    $this->position += $offset;
                    break;
                case SEEK_END:
                    $this->position = strlen($this->buffer) + $offset;
                    break;
            }


            return TRUE;
        }


        public function stream_tell()
        {
            return $this->position;
        }


        public function stream_stat()
        {
            return array('size' => strlen($this->buffer));
        }


        public function url_stat($path, $flags)
        {
            $response = $this->postRequest($path);
            return array('size' => strlen($response));
        }


        protected function postRequest($path, $authType = CURLAUTH_ANY)
        {
            $this->curlHandle = curl_init($path);
            curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($this->curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
            if(streamWrapperHttpAuth::$Username)
            {
                curl_setopt($this->curlHandle, CURLOPT_HTTPAUTH, $authType);
                curl_setopt($this->curlHandle, CURLOPT_USERPWD, streamWrapperHttpAuth::$Username . ':' . streamWrapperHttpAuth::$Password);
            }


            //curl_setopt($this->curlHandle, CURLOPT_SSLVERSION, 3);
            curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
            //curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYHOST, 2);


            $response = curl_exec($this->curlHandle);


            if(($info = curl_getinfo($this->curlHandle)) && $info['http_code']==200)
            {
                if(curl_errno($this->curlHandle)==0)
                {
                    return $response;
                }else
                    throw new Exception(curl_error($this->curlHandle), curl_errno($this->curlHandle));
            }else if($info['http_code']==401)
            { // Attempt NTLM Auth only, CURLAUTH_ANY does not work with NTML
                if($authType!=CURLAUTH_NTLM)
                    return $this->postRequest($path, CURLAUTH_NTLM);
                else
                {
                    throw new Exception ('Access Denied', 401);
                }
            }else if(curl_errno($this->curlHandle)!=0)
            {
                throw new Exception(curl_error($this->curlHandle), curl_errno($this->curlHandle));
            }else
                throw new Exception('Error', $info['http_code']);


            return FALSE;
        }
    }
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值