source: pro-violet-viettel/sourcecode/application/libraries/nusoap/class.soapclient.php @ 544

Last change on this file since 544 was 440, checked in by dungnv, 11 years ago
File size: 32.9 KB
RevLine 
[419]1<?php
2
3
4
5
6/**
7*
8* [nu]soapclient higher level class for easy usage.
9*
10* usage:
11*
12* // instantiate client with server info
13* $soapclient = new nusoap_client( string path [ ,mixed wsdl] );
14*
15* // call method, get results
16* echo $soapclient->call( string methodname [ ,array parameters] );
17*
18* // bye bye client
19* unset($soapclient);
20*
21* @author   Dietrich Ayala <dietrich@ganx4.com>
22* @author   Scott Nichol <snichol@users.sourceforge.net>
23* @version  $Id: class.soapclient.php,v 1.69 2010/04/26 20:15:08 snichol Exp $
24* @access   public
25*/
26class nusoap_client extends nusoap_base  {
27
28        var $username = '';                             // Username for HTTP authentication
29        var $password = '';                             // Password for HTTP authentication
30        var $authtype = '';                             // Type of HTTP authentication
31        var $certRequest = array();             // Certificate for HTTP SSL authentication
32        var $requestHeaders = false;    // SOAP headers in request (text)
33        var $responseHeaders = '';              // SOAP headers from response (incomplete namespace resolution) (text)
34        var $responseHeader = NULL;             // SOAP Header from response (parsed)
35        var $document = '';                             // SOAP body response portion (incomplete namespace resolution) (text)
36        var $endpoint;
37        var $forceEndpoint = '';                // overrides WSDL endpoint
38    var $proxyhost = '';
39    var $proxyport = '';
40        var $proxyusername = '';
41        var $proxypassword = '';
42        var $portName = '';                             // port name to use in WSDL
43    var $xml_encoding = '';                     // character set encoding of incoming (response) messages
44        var $http_encoding = false;
45        var $timeout = 0;                               // HTTP connection timeout
46        var $response_timeout = 30;             // HTTP response timeout
47        var $endpointType = '';                 // soap|wsdl, empty for WSDL initialization error
48        var $persistentConnection = false;
49        var $defaultRpcParams = false;  // This is no longer used
50        var $request = '';                              // HTTP request
51        var $response = '';                             // HTTP response
52        var $responseData = '';                 // SOAP payload of response
53        var $cookies = array();                 // Cookies from response or for request
54    var $decode_utf8 = true;            // toggles whether the parser decodes element content w/ utf8_decode()
55        var $operations = array();              // WSDL operations, empty for WSDL initialization error
[440]56       
57        var $operation = '';            // Dungnv added 2014.11.07
58       
[419]59        var $curl_options = array();    // User-specified cURL options
60        var $bindingType = '';                  // WSDL operation binding type
61        var $use_curl = false;                  // whether to always try to use cURL
62
63        /*
64         * fault related variables
65         */
66        /**
67         * @var      fault
68         * @access   public
69         */
70        var $fault;
71        /**
72         * @var      faultcode
73         * @access   public
74         */
75        var $faultcode;
76        /**
77         * @var      faultstring
78         * @access   public
79         */
80        var $faultstring;
81        /**
82         * @var      faultdetail
83         * @access   public
84         */
85        var $faultdetail;
86
87        /**
88        * constructor
89        *
90        * @param    mixed $endpoint SOAP server or WSDL URL (string), or wsdl instance (object)
91        * @param    mixed $wsdl optional, set to 'wsdl' or true if using WSDL
92        * @param    string $proxyhost optional
93        * @param    string $proxyport optional
94        * @param        string $proxyusername optional
95        * @param        string $proxypassword optional
96        * @param        integer $timeout set the connection timeout
97        * @param        integer $response_timeout set the response timeout
98        * @param        string $portName optional portName in WSDL document
99        * @access   public
100        */
101        function nusoap_client($endpoint,$wsdl = false,$proxyhost = false,$proxyport = false,$proxyusername = false, $proxypassword = false, $timeout = 0, $response_timeout = 30, $portName = ''){
102                parent::nusoap_base();
103                $this->endpoint = $endpoint;
104                $this->proxyhost = $proxyhost;
105                $this->proxyport = $proxyport;
106                $this->proxyusername = $proxyusername;
107                $this->proxypassword = $proxypassword;
108                $this->timeout = $timeout;
109                $this->response_timeout = $response_timeout;
110                $this->portName = $portName;
111
112                $this->debug("ctor wsdl=$wsdl timeout=$timeout response_timeout=$response_timeout");
113                $this->appendDebug('endpoint=' . $this->varDump($endpoint));
114
115                // make values
116                if($wsdl){
117                        if (is_object($endpoint) && (get_class($endpoint) == 'wsdl')) {
118                                $this->wsdl = $endpoint;
119                                $this->endpoint = $this->wsdl->wsdl;
120                                $this->wsdlFile = $this->endpoint;
121                                $this->debug('existing wsdl instance created from ' . $this->endpoint);
122                                $this->checkWSDL();
123                        } else {
124                                $this->wsdlFile = $this->endpoint;
125                                $this->wsdl = null;
126                                $this->debug('will use lazy evaluation of wsdl from ' . $this->endpoint);
127                        }
128                        $this->endpointType = 'wsdl';
129                } else {
130                        $this->debug("instantiate SOAP with endpoint at $endpoint");
131                        $this->endpointType = 'soap';
132                }
133        }
134
135        /**
136        * calls method, returns PHP native type
137        *
138        * @param    string $operation SOAP server URL or path
139        * @param    mixed $params An array, associative or simple, of the parameters
140        *                                     for the method call, or a string that is the XML
141        *                                     for the call.  For rpc style, this call will
142        *                                     wrap the XML in a tag named after the method, as
143        *                                     well as the SOAP Envelope and Body.  For document
144        *                                     style, this will only wrap with the Envelope and Body.
145        *                                     IMPORTANT: when using an array with document style,
146        *                                     in which case there
147        *                         is really one parameter, the root of the fragment
148        *                         used in the call, which encloses what programmers
149        *                         normally think of parameters.  A parameter array
150        *                         *must* include the wrapper.
151        * @param        string $namespace optional method namespace (WSDL can override)
152        * @param        string $soapAction optional SOAPAction value (WSDL can override)
153        * @param        mixed $headers optional string of XML with SOAP header content, or array of soapval objects for SOAP headers, or associative array
154        * @param        boolean $rpcParams optional (no longer used)
155        * @param        string  $style optional (rpc|document) the style to use when serializing parameters (WSDL can override)
156        * @param        string  $use optional (encoded|literal) the use when serializing parameters (WSDL can override)
157        * @return       mixed   response from SOAP call, normally an associative array mirroring the structure of the XML response, false for certain fatal errors
158        * @access   public
159        */
160        function call($operation,$params=array(),$namespace='http://tempuri.org',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){
161                $this->operation = $operation;
162                $this->fault = false;
163                $this->setError('');
164                $this->request = '';
165                $this->response = '';
166                $this->responseData = '';
167                $this->faultstring = '';
168                $this->faultcode = '';
169                $this->opData = array();
170               
171                $this->debug("call: operation=$operation, namespace=$namespace, soapAction=$soapAction, rpcParams=$rpcParams, style=$style, use=$use, endpointType=$this->endpointType");
172                $this->appendDebug('params=' . $this->varDump($params));
173                $this->appendDebug('headers=' . $this->varDump($headers));
174                if ($headers) {
175                        $this->requestHeaders = $headers;
176                }
177                if ($this->endpointType == 'wsdl' && is_null($this->wsdl)) {
178                        $this->loadWSDL();
179                        if ($this->getError())
180                                return false;
181                }
182                // serialize parameters
183                if($this->endpointType == 'wsdl' && $opData = $this->getOperationData($operation)){
184                        // use WSDL for operation
185                        $this->opData = $opData;
186                        $this->debug("found operation");
187                        $this->appendDebug('opData=' . $this->varDump($opData));
188                        if (isset($opData['soapAction'])) {
189                                $soapAction = $opData['soapAction'];
190                        }
191                        if (! $this->forceEndpoint) {
192                                $this->endpoint = $opData['endpoint'];
193                        } else {
194                                $this->endpoint = $this->forceEndpoint;
195                        }
196                        $namespace = isset($opData['input']['namespace']) ? $opData['input']['namespace'] :     $namespace;
197                        $style = $opData['style'];
198                        $use = $opData['input']['use'];
199                        // add ns to ns array
200                        if($namespace != '' && !isset($this->wsdl->namespaces[$namespace])){
201                                $nsPrefix = 'ns' . rand(1000, 9999);
202                                $this->wsdl->namespaces[$nsPrefix] = $namespace;
203                        }
204            $nsPrefix = $this->wsdl->getPrefixFromNamespace($namespace);
205                        // serialize payload
206                        if (is_string($params)) {
207                                $this->debug("serializing param string for WSDL operation $operation");
208                                $payload = $params;
209                        } elseif (is_array($params)) {
210                                $this->debug("serializing param array for WSDL operation $operation");
211                                $payload = $this->wsdl->serializeRPCParameters($operation,'input',$params,$this->bindingType);
212                        } else {
213                                $this->debug('params must be array or string');
214                                $this->setError('params must be array or string');
215                                return false;
216                        }
217            $usedNamespaces = $this->wsdl->usedNamespaces;
218                        if (isset($opData['input']['encodingStyle'])) {
219                                $encodingStyle = $opData['input']['encodingStyle'];
220                        } else {
221                                $encodingStyle = '';
222                        }
223                        $this->appendDebug($this->wsdl->getDebug());
224                        $this->wsdl->clearDebug();
225                        if ($errstr = $this->wsdl->getError()) {
226                                $this->debug('got wsdl error: '.$errstr);
227                                $this->setError('wsdl error: '.$errstr);
228                                return false;
229                        }
230                } elseif($this->endpointType == 'wsdl') {
231                        // operation not in WSDL
232                        $this->appendDebug($this->wsdl->getDebug());
233                        $this->wsdl->clearDebug();
234                        $this->setError('operation '.$operation.' not present in WSDL.');
235                        $this->debug("operation '$operation' not present in WSDL.");
236                        return false;
237                } else {
238                        // no WSDL
239                        //$this->namespaces['ns1'] = $namespace;
240                        $nsPrefix = 'ns' . rand(1000, 9999);
241                        // serialize
242                        $payload = '';
243                        if (is_string($params)) {
244                                $this->debug("serializing param string for operation $operation");
245                                $payload = $params;
246                        } elseif (is_array($params)) {
247                                $this->debug("serializing param array for operation $operation");
248                                foreach($params as $k => $v){
249                                        $payload .= $this->serialize_val($v,$k,false,false,false,false,$use);
250                                }
251                        } else {
252                                $this->debug('params must be array or string');
253                                $this->setError('params must be array or string');
254                                return false;
255                        }
256                        $usedNamespaces = array();
257                        if ($use == 'encoded') {
258                                $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
259                        } else {
260                                $encodingStyle = '';
261                        }
262                }
263                // wrap RPC calls with method element
264                if ($style == 'rpc') {
265                        if ($use == 'literal') {
266                                $this->debug("wrapping RPC request with literal method element");
267                                if ($namespace) {
268                                        // http://www.ws-i.org/Profiles/BasicProfile-1.1-2004-08-24.html R2735 says rpc/literal accessor elements should not be in a namespace
269                                        $payload = "<$nsPrefix:$operation xmlns:$nsPrefix=\"$namespace\">" .
270                                                                $payload .
271                                                                "</$nsPrefix:$operation>";
272                                } else {
273                                        $payload = "<$operation>" . $payload . "</$operation>";
274                                }
275                        } else {
276                                $this->debug("wrapping RPC request with encoded method element");
277                                if ($namespace) {
278                                        $payload = "<$nsPrefix:$operation xmlns:$nsPrefix=\"$namespace\">" .
279                                                                $payload .
280                                                                "</$nsPrefix:$operation>";
281                                } else {
282                                        $payload = "<$operation>" .
283                                                                $payload .
284                                                                "</$operation>";
285                                }
286                        }
287                }
288                // serialize envelope
289                $soapmsg = $this->serializeEnvelope($payload,$this->requestHeaders,$usedNamespaces,$style,$use,$encodingStyle);
290                $this->debug("endpoint=$this->endpoint, soapAction=$soapAction, namespace=$namespace, style=$style, use=$use, encodingStyle=$encodingStyle");
291                $this->debug('SOAP message length=' . strlen($soapmsg) . ' contents (max 1000 bytes)=' . substr($soapmsg, 0, 1000));
292                // send
293                $return = $this->send($this->getHTTPBody($soapmsg),$soapAction,$this->timeout,$this->response_timeout);
294                if($errstr = $this->getError()){
295                        $this->debug('Error: '.$errstr);
296                        return false;
297                } else {
298                        $this->return = $return;
299                        $this->debug('sent message successfully and got a(n) '.gettype($return));
300                $this->appendDebug('return=' . $this->varDump($return));
301                       
302                        // fault?
303                        if(is_array($return) && isset($return['faultcode'])){
304                                $this->debug('got fault');
305                                $this->setError($return['faultcode'].': '.$return['faultstring']);
306                                $this->fault = true;
307                                foreach($return as $k => $v){
308                                        $this->$k = $v;
309                                        $this->debug("$k = $v<br>");
310                                }
311                                return $return;
312                        } elseif ($style == 'document') {
313                                // NOTE: if the response is defined to have multiple parts (i.e. unwrapped),
314                                // we are only going to return the first part here...sorry about that
315                                return $return;
316                        } else {
317                                // array of return values
318                                if(is_array($return)){
319                                        // multiple 'out' parameters, which we return wrapped up
320                                        // in the array
321                                        if(sizeof($return) > 1){
322                                                return $return;
323                                        }
324                                        // single 'out' parameter (normally the return value)
325                                        $return = array_shift($return);
326                                        $this->debug('return shifted value: ');
327                                        $this->appendDebug($this->varDump($return));
328                                return $return;
329                                // nothing returned (ie, echoVoid)
330                                } else {
331                                        return "";
332                                }
333                        }
334                }
335        }
336
337        /**
338        * check WSDL passed as an instance or pulled from an endpoint
339        *
340        * @access   private
341        */
342        function checkWSDL() {
343                $this->appendDebug($this->wsdl->getDebug());
344                $this->wsdl->clearDebug();
345                $this->debug('checkWSDL');
346                // catch errors
347                if ($errstr = $this->wsdl->getError()) {
348                        $this->appendDebug($this->wsdl->getDebug());
349                        $this->wsdl->clearDebug();
350                        $this->debug('got wsdl error: '.$errstr);
351                        $this->setError('wsdl error: '.$errstr);
352                } elseif ($this->operations = $this->wsdl->getOperations($this->portName, 'soap')) {
353                        $this->appendDebug($this->wsdl->getDebug());
354                        $this->wsdl->clearDebug();
355                        $this->bindingType = 'soap';
356                        $this->debug('got '.count($this->operations).' operations from wsdl '.$this->wsdlFile.' for binding type '.$this->bindingType);
357                } elseif ($this->operations = $this->wsdl->getOperations($this->portName, 'soap12')) {
358                        $this->appendDebug($this->wsdl->getDebug());
359                        $this->wsdl->clearDebug();
360                        $this->bindingType = 'soap12';
361                        $this->debug('got '.count($this->operations).' operations from wsdl '.$this->wsdlFile.' for binding type '.$this->bindingType);
362                        $this->debug('**************** WARNING: SOAP 1.2 BINDING *****************');
363                } else {
364                        $this->appendDebug($this->wsdl->getDebug());
365                        $this->wsdl->clearDebug();
366                        $this->debug('getOperations returned false');
367                        $this->setError('no operations defined in the WSDL document!');
368                }
369        }
370
371        /**
372         * instantiate wsdl object and parse wsdl file
373         *
374         * @access      public
375         */
376        function loadWSDL() {
377                $this->debug('instantiating wsdl class with doc: '.$this->wsdlFile);
378                $this->wsdl = new wsdl('',$this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword,$this->timeout,$this->response_timeout,$this->curl_options,$this->use_curl);
379                $this->wsdl->setCredentials($this->username, $this->password, $this->authtype, $this->certRequest);
380                $this->wsdl->fetchWSDL($this->wsdlFile);
381                $this->checkWSDL();
382        }
383
384        /**
385        * get available data pertaining to an operation
386        *
387        * @param    string $operation operation name
388        * @return       array array of data pertaining to the operation
389        * @access   public
390        */
391        function getOperationData($operation){
392                if ($this->endpointType == 'wsdl' && is_null($this->wsdl)) {
393                        $this->loadWSDL();
394                        if ($this->getError())
395                                return false;
396                }
397                if(isset($this->operations[$operation])){
398                        return $this->operations[$operation];
399                }
400                $this->debug("No data for operation: $operation");
401        }
402
403    /**
404    * send the SOAP message
405    *
406    * Note: if the operation has multiple return values
407    * the return value of this method will be an array
408    * of those values.
409    *
410        * @param    string $msg a SOAPx4 soapmsg object
411        * @param    string $soapaction SOAPAction value
412        * @param    integer $timeout set connection timeout in seconds
413        * @param        integer $response_timeout set response timeout in seconds
414        * @return       mixed native PHP types.
415        * @access   private
416        */
417        function send($msg, $soapaction = '', $timeout=0, $response_timeout=30) {
418                $this->checkCookies();
419                // detect transport
420                switch(true){
421                        // http(s)
422                        case preg_match('/^http/',$this->endpoint):
423                                $this->debug('transporting via HTTP');
424                                if($this->persistentConnection == true && is_object($this->persistentConnection)){
425                                        $http =& $this->persistentConnection;
426                                } else {
427                                        $http = new soap_transport_http($this->endpoint, $this->curl_options, $this->use_curl);
428                                        if ($this->persistentConnection) {
429                                                $http->usePersistentConnection();
430                                        }
431                                }
432                                $http->setContentType($this->getHTTPContentType(), $this->getHTTPContentTypeCharset());
433                                $http->setSOAPAction($soapaction);
434                                if($this->proxyhost && $this->proxyport){
435                                        $http->setProxy($this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword);
436                                }
437                if($this->authtype != '') {
438                                        $http->setCredentials($this->username, $this->password, $this->authtype, array(), $this->certRequest);
439                                }
440                                if($this->http_encoding != ''){
441                                        $http->setEncoding($this->http_encoding);
442                                }
443                                $this->debug('sending message, length='.strlen($msg));
444                                if(preg_match('/^http:/',$this->endpoint)){
445                                //if(strpos($this->endpoint,'http:')){
446                                        $this->responseData = $http->send($msg,$timeout,$response_timeout,$this->cookies);
447                                } elseif(preg_match('/^https/',$this->endpoint)){
448                                //} elseif(strpos($this->endpoint,'https:')){
449                                        //if(phpversion() == '4.3.0-dev'){
450                                                //$response = $http->send($msg,$timeout,$response_timeout);
451                                //$this->request = $http->outgoing_payload;
452                                                //$this->response = $http->incoming_payload;
453                                        //} else
454                                        $this->responseData = $http->sendHTTPS($msg,$timeout,$response_timeout,$this->cookies);
455                                } else {
456                                        $this->setError('no http/s in endpoint url');
457                                }
458                                $this->request = $http->outgoing_payload;
459                                $this->response = $http->incoming_payload;
460                                $this->appendDebug($http->getDebug());
461                                $this->UpdateCookies($http->incoming_cookies);
462
463                                // save transport object if using persistent connections
464                                if ($this->persistentConnection) {
465                                        $http->clearDebug();
466                                        if (!is_object($this->persistentConnection)) {
467                                                $this->persistentConnection = $http;
468                                        }
469                                }
470                               
471                                if($err = $http->getError()){
472                                        $this->setError('HTTP Error: '.$err);
473                                        return false;
474                                } elseif($this->getError()){
475                                        return false;
476                                } else {
477                                        $this->debug('got response, length='. strlen($this->responseData).' type='.$http->incoming_headers['content-type']);
478                                        return $this->parseResponse($http->incoming_headers, $this->responseData);
479                                }
480                        break;
481                        default:
482                                $this->setError('no transport found, or selected transport is not yet supported!');
483                        return false;
484                        break;
485                }
486        }
487
488        /**
489        * processes SOAP message returned from server
490        *
491        * @param        array   $headers        The HTTP headers
492        * @param        string  $data           unprocessed response data from server
493        * @return       mixed   value of the message, decoded into a PHP type
494        * @access   private
495        */
496    function parseResponse($headers, $data) {
497                $this->debug('Entering parseResponse() for data of length ' . strlen($data) . ' headers:');
498                $this->appendDebug($this->varDump($headers));
499        if (!isset($headers['content-type'])) {
500                        $this->setError('Response not of type text/xml (no content-type header)');
501                        return false;
502        }
503                if (!strstr($headers['content-type'], 'text/xml')) {
504                        $this->setError('Response not of type text/xml: ' . $headers['content-type']);
505                        return false;
506                }
507                if (strpos($headers['content-type'], '=')) {
508                        $enc = str_replace('"', '', substr(strstr($headers["content-type"], '='), 1));
509                        $this->debug('Got response encoding: ' . $enc);
510                        if(preg_match('/^(ISO-8859-1|US-ASCII|UTF-8)$/i',$enc)){
511                                $this->xml_encoding = strtoupper($enc);
512                        } else {
513                                $this->xml_encoding = 'US-ASCII';
514                        }
515                } else {
516                        // should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
517                        $this->xml_encoding = 'ISO-8859-1';
518                }
519                $this->debug('Use encoding: ' . $this->xml_encoding . ' when creating nusoap_parser');
520                $parser = new nusoap_parser($data,$this->xml_encoding,$this->operation,$this->decode_utf8);
521                // add parser debug data to our debug
522                $this->appendDebug($parser->getDebug());
523                // if parse errors
524                if($errstr = $parser->getError()){
525                        $this->setError( $errstr);
526                        // destroy the parser object
527                        unset($parser);
528                        return false;
529                } else {
530                        // get SOAP headers
531                        $this->responseHeaders = $parser->getHeaders();
532                        // get SOAP headers
533                        $this->responseHeader = $parser->get_soapheader();
534                        // get decoded message
535                        $return = $parser->get_soapbody();
536            // add document for doclit support
537            $this->document = $parser->document;
538                        // destroy the parser object
539                        unset($parser);
540                        // return decode message
541                        return $return;
542                }
543         }
544
545        /**
546        * sets user-specified cURL options
547        *
548        * @param        mixed $option The cURL option (always integer?)
549        * @param        mixed $value The cURL option value
550        * @access   public
551        */
552        function setCurlOption($option, $value) {
553                $this->debug("setCurlOption option=$option, value=");
554                $this->appendDebug($this->varDump($value));
555                $this->curl_options[$option] = $value;
556        }
557
558        /**
559        * sets the SOAP endpoint, which can override WSDL
560        *
561        * @param        string $endpoint The endpoint URL to use, or empty string or false to prevent override
562        * @access   public
563        */
564        function setEndpoint($endpoint) {
565                $this->debug("setEndpoint(\"$endpoint\")");
566                $this->forceEndpoint = $endpoint;
567        }
568
569        /**
570        * set the SOAP headers
571        *
572        * @param        mixed $headers String of XML with SOAP header content, or array of soapval objects for SOAP headers
573        * @access   public
574        */
575        function setHeaders($headers){
576                $this->debug("setHeaders headers=");
577                $this->appendDebug($this->varDump($headers));
578                $this->requestHeaders = $headers;
579        }
580
581        /**
582        * get the SOAP response headers (namespace resolution incomplete)
583        *
584        * @return       string
585        * @access   public
586        */
587        function getHeaders(){
588                return $this->responseHeaders;
589        }
590
591        /**
592        * get the SOAP response Header (parsed)
593        *
594        * @return       mixed
595        * @access   public
596        */
597        function getHeader(){
598                return $this->responseHeader;
599        }
600
601        /**
602        * set proxy info here
603        *
604        * @param    string $proxyhost
605        * @param    string $proxyport
606        * @param        string $proxyusername
607        * @param        string $proxypassword
608        * @access   public
609        */
610        function setHTTPProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '') {
611                $this->proxyhost = $proxyhost;
612                $this->proxyport = $proxyport;
613                $this->proxyusername = $proxyusername;
614                $this->proxypassword = $proxypassword;
615        }
616
617        /**
618        * if authenticating, set user credentials here
619        *
620        * @param    string $username
621        * @param    string $password
622        * @param        string $authtype (basic|digest|certificate|ntlm)
623        * @param        array $certRequest (keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, verifypeer (optional), verifyhost (optional): see corresponding options in cURL docs)
624        * @access   public
625        */
626        function setCredentials($username, $password, $authtype = 'basic', $certRequest = array()) {
627                $this->debug("setCredentials username=$username authtype=$authtype certRequest=");
628                $this->appendDebug($this->varDump($certRequest));
629                $this->username = $username;
630                $this->password = $password;
631                $this->authtype = $authtype;
632                $this->certRequest = $certRequest;
633        }
634       
635        /**
636        * use HTTP encoding
637        *
638        * @param    string $enc HTTP encoding
639        * @access   public
640        */
641        function setHTTPEncoding($enc='gzip, deflate'){
642                $this->debug("setHTTPEncoding(\"$enc\")");
643                $this->http_encoding = $enc;
644        }
645       
646        /**
647        * Set whether to try to use cURL connections if possible
648        *
649        * @param        boolean $use Whether to try to use cURL
650        * @access   public
651        */
652        function setUseCURL($use) {
653                $this->debug("setUseCURL($use)");
654                $this->use_curl = $use;
655        }
656
657        /**
658        * use HTTP persistent connections if possible
659        *
660        * @access   public
661        */
662        function useHTTPPersistentConnection(){
663                $this->debug("useHTTPPersistentConnection");
664                $this->persistentConnection = true;
665        }
666       
667        /**
668        * gets the default RPC parameter setting.
669        * If true, default is that call params are like RPC even for document style.
670        * Each call() can override this value.
671        *
672        * This is no longer used.
673        *
674        * @return boolean
675        * @access public
676        * @deprecated
677        */
678        function getDefaultRpcParams() {
679                return $this->defaultRpcParams;
680        }
681
682        /**
683        * sets the default RPC parameter setting.
684        * If true, default is that call params are like RPC even for document style
685        * Each call() can override this value.
686        *
687        * This is no longer used.
688        *
689        * @param    boolean $rpcParams
690        * @access public
691        * @deprecated
692        */
693        function setDefaultRpcParams($rpcParams) {
694                $this->defaultRpcParams = $rpcParams;
695        }
696       
697        /**
698        * dynamically creates an instance of a proxy class,
699        * allowing user to directly call methods from wsdl
700        *
701        * @return   object soap_proxy object
702        * @access   public
703        */
704        function getProxy() {
705                $r = rand();
706                $evalStr = $this->_getProxyClassCode($r);
707                //$this->debug("proxy class: $evalStr");
708                if ($this->getError()) {
709                        $this->debug("Error from _getProxyClassCode, so return NULL");
710                        return null;
711                }
712                // eval the class
713                eval($evalStr);
714                // instantiate proxy object
715                eval("\$proxy = new nusoap_proxy_$r('');");
716                // transfer current wsdl data to the proxy thereby avoiding parsing the wsdl twice
717                $proxy->endpointType = 'wsdl';
718                $proxy->wsdlFile = $this->wsdlFile;
719                $proxy->wsdl = $this->wsdl;
720                $proxy->operations = $this->operations;
721                $proxy->defaultRpcParams = $this->defaultRpcParams;
722                // transfer other state
723                $proxy->soap_defencoding = $this->soap_defencoding;
724                $proxy->username = $this->username;
725                $proxy->password = $this->password;
726                $proxy->authtype = $this->authtype;
727                $proxy->certRequest = $this->certRequest;
728                $proxy->requestHeaders = $this->requestHeaders;
729                $proxy->endpoint = $this->endpoint;
730                $proxy->forceEndpoint = $this->forceEndpoint;
731                $proxy->proxyhost = $this->proxyhost;
732                $proxy->proxyport = $this->proxyport;
733                $proxy->proxyusername = $this->proxyusername;
734                $proxy->proxypassword = $this->proxypassword;
735                $proxy->http_encoding = $this->http_encoding;
736                $proxy->timeout = $this->timeout;
737                $proxy->response_timeout = $this->response_timeout;
738                $proxy->persistentConnection = &$this->persistentConnection;
739                $proxy->decode_utf8 = $this->decode_utf8;
740                $proxy->curl_options = $this->curl_options;
741                $proxy->bindingType = $this->bindingType;
742                $proxy->use_curl = $this->use_curl;
743                return $proxy;
744        }
745
746        /**
747        * dynamically creates proxy class code
748        *
749        * @return   string PHP/NuSOAP code for the proxy class
750        * @access   private
751        */
752        function _getProxyClassCode($r) {
753                $this->debug("in getProxy endpointType=$this->endpointType");
754                $this->appendDebug("wsdl=" . $this->varDump($this->wsdl));
755                if ($this->endpointType != 'wsdl') {
756                        $evalStr = 'A proxy can only be created for a WSDL client';
757                        $this->setError($evalStr);
758                        $evalStr = "echo \"$evalStr\";";
759                        return $evalStr;
760                }
761                if ($this->endpointType == 'wsdl' && is_null($this->wsdl)) {
762                        $this->loadWSDL();
763                        if ($this->getError()) {
764                                return "echo \"" . $this->getError() . "\";";
765                        }
766                }
767                $evalStr = '';
768                foreach ($this->operations as $operation => $opData) {
769                        if ($operation != '') {
770                                // create param string and param comment string
771                                if (sizeof($opData['input']['parts']) > 0) {
772                                        $paramStr = '';
773                                        $paramArrayStr = '';
774                                        $paramCommentStr = '';
775                                        foreach ($opData['input']['parts'] as $name => $type) {
776                                                $paramStr .= "\$$name, ";
777                                                $paramArrayStr .= "'$name' => \$$name, ";
778                                                $paramCommentStr .= "$type \$$name, ";
779                                        }
780                                        $paramStr = substr($paramStr, 0, strlen($paramStr)-2);
781                                        $paramArrayStr = substr($paramArrayStr, 0, strlen($paramArrayStr)-2);
782                                        $paramCommentStr = substr($paramCommentStr, 0, strlen($paramCommentStr)-2);
783                                } else {
784                                        $paramStr = '';
785                                        $paramArrayStr = '';
786                                        $paramCommentStr = 'void';
787                                }
788                                $opData['namespace'] = !isset($opData['namespace']) ? 'http://testuri.com' : $opData['namespace'];
789                                $evalStr .= "// $paramCommentStr
790        function " . str_replace('.', '__', $operation) . "($paramStr) {
791                \$params = array($paramArrayStr);
792                return \$this->call('$operation', \$params, '".$opData['namespace']."', '".(isset($opData['soapAction']) ? $opData['soapAction'] : '')."');
793        }
794        ";
795                                unset($paramStr);
796                                unset($paramCommentStr);
797                        }
798                }
799                $evalStr = 'class nusoap_proxy_'.$r.' extends nusoap_client {
800        '.$evalStr.'
801}';
802                return $evalStr;
803        }
804
805        /**
806        * dynamically creates proxy class code
807        *
808        * @return   string PHP/NuSOAP code for the proxy class
809        * @access   public
810        */
811        function getProxyClassCode() {
812                $r = rand();
813                return $this->_getProxyClassCode($r);
814        }
815
816        /**
817        * gets the HTTP body for the current request.
818        *
819        * @param string $soapmsg The SOAP payload
820        * @return string The HTTP body, which includes the SOAP payload
821        * @access private
822        */
823        function getHTTPBody($soapmsg) {
824                return $soapmsg;
825        }
826       
827        /**
828        * gets the HTTP content type for the current request.
829        *
830        * Note: getHTTPBody must be called before this.
831        *
832        * @return string the HTTP content type for the current request.
833        * @access private
834        */
835        function getHTTPContentType() {
836                return 'text/xml';
837        }
838       
839        /**
840        * gets the HTTP content type charset for the current request.
841        * returns false for non-text content types.
842        *
843        * Note: getHTTPBody must be called before this.
844        *
845        * @return string the HTTP content type charset for the current request.
846        * @access private
847        */
848        function getHTTPContentTypeCharset() {
849                return $this->soap_defencoding;
850        }
851
852        /*
853        * whether or not parser should decode utf8 element content
854    *
855    * @return   always returns true
856    * @access   public
857    */
858    function decodeUTF8($bool){
859                $this->decode_utf8 = $bool;
860                return true;
861    }
862
863        /**
864         * adds a new Cookie into $this->cookies array
865         *
866         * @param       string $name Cookie Name
867         * @param       string $value Cookie Value
868         * @return      boolean if cookie-set was successful returns true, else false
869         * @access      public
870         */
871        function setCookie($name, $value) {
872                if (strlen($name) == 0) {
873                        return false;
874                }
875                $this->cookies[] = array('name' => $name, 'value' => $value);
876                return true;
877        }
878
879        /**
880         * gets all Cookies
881         *
882         * @return   array with all internal cookies
883         * @access   public
884         */
885        function getCookies() {
886                return $this->cookies;
887        }
888
889        /**
890         * checks all Cookies and delete those which are expired
891         *
892         * @return   boolean always return true
893         * @access   private
894         */
895        function checkCookies() {
896                if (sizeof($this->cookies) == 0) {
897                        return true;
898                }
899                $this->debug('checkCookie: check ' . sizeof($this->cookies) . ' cookies');
900                $curr_cookies = $this->cookies;
901                $this->cookies = array();
902                foreach ($curr_cookies as $cookie) {
903                        if (! is_array($cookie)) {
904                                $this->debug('Remove cookie that is not an array');
905                                continue;
906                        }
907                        if ((isset($cookie['expires'])) && (! empty($cookie['expires']))) {
908                                if (strtotime($cookie['expires']) > time()) {
909                                        $this->cookies[] = $cookie;
910                                } else {
911                                        $this->debug('Remove expired cookie ' . $cookie['name']);
912                                }
913                        } else {
914                                $this->cookies[] = $cookie;
915                        }
916                }
917                $this->debug('checkCookie: '.sizeof($this->cookies).' cookies left in array');
918                return true;
919        }
920
921        /**
922         * updates the current cookies with a new set
923         *
924         * @param       array $cookies new cookies with which to update current ones
925         * @return      boolean always return true
926         * @access      private
927         */
928        function UpdateCookies($cookies) {
929                if (sizeof($this->cookies) == 0) {
930                        // no existing cookies: take whatever is new
931                        if (sizeof($cookies) > 0) {
932                                $this->debug('Setting new cookie(s)');
933                                $this->cookies = $cookies;
934                        }
935                        return true;
936                }
937                if (sizeof($cookies) == 0) {
938                        // no new cookies: keep what we've got
939                        return true;
940                }
941                // merge
942                foreach ($cookies as $newCookie) {
943                        if (!is_array($newCookie)) {
944                                continue;
945                        }
946                        if ((!isset($newCookie['name'])) || (!isset($newCookie['value']))) {
947                                continue;
948                        }
949                        $newName = $newCookie['name'];
950
951                        $found = false;
952                        for ($i = 0; $i < count($this->cookies); $i++) {
953                                $cookie = $this->cookies[$i];
954                                if (!is_array($cookie)) {
955                                        continue;
956                                }
957                                if (!isset($cookie['name'])) {
958                                        continue;
959                                }
960                                if ($newName != $cookie['name']) {
961                                        continue;
962                                }
963                                $newDomain = isset($newCookie['domain']) ? $newCookie['domain'] : 'NODOMAIN';
964                                $domain = isset($cookie['domain']) ? $cookie['domain'] : 'NODOMAIN';
965                                if ($newDomain != $domain) {
966                                        continue;
967                                }
968                                $newPath = isset($newCookie['path']) ? $newCookie['path'] : 'NOPATH';
969                                $path = isset($cookie['path']) ? $cookie['path'] : 'NOPATH';
970                                if ($newPath != $path) {
971                                        continue;
972                                }
973                                $this->cookies[$i] = $newCookie;
974                                $found = true;
975                                $this->debug('Update cookie ' . $newName . '=' . $newCookie['value']);
976                                break;
977                        }
978                        if (! $found) {
979                                $this->debug('Add cookie ' . $newName . '=' . $newCookie['value']);
980                                $this->cookies[] = $newCookie;
981                        }
982                }
983                return true;
984        }
985}
986
987if (!extension_loaded('soap')) {
988        /**
989         *      For backwards compatiblity, define soapclient unless the PHP SOAP extension is loaded.
990         */
991        class soapclient extends nusoap_client {
992        }
993}
994?>
Note: See TracBrowser for help on using the repository browser.