Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.02 of the PHP license,      |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more   |
17
// | Authors: Dietrich Ayala <dietrich@ganx4.com> Original Author         |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: Client.php 696 2011-09-08 09:08:23Z tiefland $
21
//
22
 
23
require_once 'PayPal/SOAP/Value.php';
24
require_once 'PayPal/SOAP/Base.php';
25
require_once 'PayPal/SOAP/Transport.php';
26
require_once 'PayPal/SOAP/WSDL.php';
27
require_once 'PayPal/SOAP/Fault.php';
28
require_once 'PayPal/SOAP/Parser.php';
29
 
30
// Arnaud: the following code was taken from DataObject
31
// and adapted to suit
32
 
33
// this will be horrifically slow!!!!
34
// NOTE: Overload SEGFAULTS ON PHP4 + Zend Optimizer
35
// these two are BC/FC handlers for call in PHP4/5
36
 
37
if (!class_exists('SOAP_Client_Overload')) {
38
    if (substr(phpversion(), 0, 1) == 5) {
39
        class SOAP_Client_Overload extends SOAP_Base
40
        {
41
            function __call($method, $args)
42
            {
43
                $return = null;
44
                $this->_call($method, $args, $return);
45
                return $return;
46
            }
47
        }
48
    } else {
49
        if (!function_exists('clone')) {
50
            eval('function clone($t) { return $t; }');
51
        }
52
        eval('
53
            class SOAP_Client_Overload extends SOAP_Base
54
            {
55
                function __call($method, $args, &$return)
56
                {
57
                    return $this->_call($method, $args, $return);
58
                }
59
            }');
60
    }
61
}
62
 
63
/**
64
 * SOAP Client Class
65
 *
66
 * This class is the main interface for making soap requests.
67
 *
68
 * basic usage:
69
 *   $soapclient = new SOAP_Client( string path [ , boolean wsdl] );
70
 *   echo $soapclient->call( string methodname [ , array parameters] );
71
 *
72
 * Originally based on SOAPx4 by Dietrich Ayala
73
 * http://dietrich.ganx4.com/soapx4
74
 *
75
 * @access   public
76
 * @package  SOAP::Client
77
 * @author   Shane Caraveo <shane@php.net> Conversion to PEAR and updates
78
 * @author   Stig Bakken <ssb@fast.no> Conversion to PEAR
79
 * @author   Dietrich Ayala <dietrich@ganx4.com> Original Author
80
 */
81
class SOAP_Client extends SOAP_Client_Overload
82
{
83
    /**
84
     * Communication endpoint.
85
     *
86
     * Currently the following transport formats are supported:
87
     *  - HTTP
88
     *  - SMTP
89
     *
90
     * Example endpoints:
91
     *   http://www.example.com/soap/server.php
92
     *   https://www.example.com/soap/server.php
93
     *   mailto:soap@example.com
94
     *
95
     * @var  string
96
     * @see  SOAP_Client()
97
     */
98
    var $_endpoint = '';
99
 
100
    /**
101
     * portname
102
     *
103
     * @var string contains the SOAP PORT name that is used by the client
104
     */
105
    var $_portName = '';
106
 
107
    /**
108
     * Endpoint type
109
     *
110
     * @var  string  e.g. wdsl
111
     */
112
    var $__endpointType = '';
113
 
114
    /**
115
     * wire
116
     *
117
     * @var  string  contains outoing and incoming data stream for debugging.
118
     */
119
    var $xml; // contains the received xml
120
    var $wire;
121
    var $__last_request = null;
122
    var $__last_response = null;
123
 
124
    /**
125
     * Options
126
     *
127
     * @var array
128
     */
129
    var $__options = array('trace'=>0);
130
 
131
    /**
132
     * encoding
133
     *
134
     * @var  string  Contains the character encoding used for XML parser, etc.
135
     */
136
    var $_encoding = SOAP_DEFAULT_ENCODING;
137
 
138
    /**
139
     * headersOut
140
     *
141
     * @var  array  contains an array of SOAP_Headers that we are sending
142
     */
143
    var $headersOut = null;
144
 
145
    /**
146
     * headersOut
147
     *
148
     * @var  array  contains an array headers we recieved back in the response
149
     */
150
    var $headersIn = null;
151
 
152
    /**
153
     * __proxy_params
154
     *
155
     * @var  array  contains options for HTTP_Request class (see HTTP/Request.php)
156
     */
157
    var $__proxy_params = array();
158
 
159
    var $_soap_transport = null;
160
 
161
    /**
162
     * SOAP_Client constructor
163
     *
164
     * @param string endpoint (URL)
165
     * @param boolean wsdl (true if endpoint is a wsdl file)
166
     * @param string portName
167
     * @param array  contains options for HTTP_Request class (see HTTP/Request.php)
168
     * @access public
169
     */
170
    function SOAP_Client($endpoint, $wsdl = false, $portName = false, $proxy_params=array())
171
    {
172
        parent::SOAP_Base('Client');
173
        $this->_endpoint = $endpoint;
174
        $this->_portName = $portName;
175
        $this->__proxy_params = $proxy_params;
176
 
177
        $wsdl = $wsdl ? $wsdl : strcasecmp('wsdl', substr($endpoint, strlen($endpoint) - 4)) == 0;
178
 
179
        // make values
180
        if ($wsdl) {
181
            $this->__endpointType = 'wsdl';
182
            // instantiate wsdl class
183
            $this->_wsdl =& new SOAP_WSDL($this->_endpoint, $this->__proxy_params);
184
            if ($this->_wsdl->fault) {
185
                $this->_raiseSoapFault($this->_wsdl->fault);
186
            }
187
        }
188
    }
189
 
190
    function _reset()
191
    {
192
        $this->xml = null;
193
        $this->wire = null;
194
        $this->__last_request = null;
195
        $this->__last_response = null;
196
        $this->headersIn = null;
197
        $this->headersOut = null;
198
    }
199
 
200
    /**
201
     * setEncoding
202
     *
203
     * set the character encoding, limited to 'UTF-8', 'US_ASCII' and 'ISO-8859-1'
204
     *
205
     * @param string encoding
206
     * @return mixed returns null or SOAP_Fault
207
     * @access public
208
     */
209
    function setEncoding($encoding)
210
    {
211
        if (in_array($encoding, $this->_encodings)) {
212
            $this->_encoding = $encoding;
213
            return null;
214
        }
215
        return $this->_raiseSoapFault('Invalid Encoding');
216
    }
217
 
218
    /**
219
     * addHeader
220
     *
221
     * To add headers to the envelop, you use this function, sending it a
222
     * SOAP_Header class instance.
223
     *
224
     * @param SOAP_Header a soap value to send as a header
225
     * @access public
226
     */
227
    function addHeader(&$soap_value)
228
    {
229
        # add a new header to the message
230
        if (is_a($soap_value,'soap_header')) {
231
            $this->headersOut[] =& $soap_value;
232
        } else if (gettype($soap_value) == 'array') {
233
            // name, value, namespace, mustunderstand, actor
234
            $this->headersOut[] =& new SOAP_Header($soap_value[0], null, $soap_value[1], $soap_value[2], $soap_value[3]);;
235
        } else {
236
            $this->_raiseSoapFault("Don't understand the header info you provided.  Must be array or SOAP_Header.");
237
        }
238
    }
239
 
240
    /**
241
     * SOAP_Client::call
242
     *
243
     * the namespace parameter is overloaded to accept an array of
244
     * options that can contain data necessary for various transports
245
     * if it is used as an array, it MAY contain a namespace value and a
246
     * soapaction value.  If it is overloaded, the soapaction parameter is
247
     * ignored and MUST be placed in the options array.  This is done
248
     * to provide backwards compatibility with current clients, but
249
     * may be removed in the future.
250
     *
251
     * @param string method
252
     * @param array  params
253
     * @param array options (hash with namespace, soapaction, timeout, from, subject, etc.)
254
     *
255
     * The options parameter can have a variety of values added.  The currently supported
256
     * values are:
257
     *   namespace
258
     *   soapaction
259
     *   timeout (http socket timeout)
260
     *   from (smtp)
261
     *   transfer-encoding (smtp, sets the Content-Transfer-Encoding header)
262
     *   subject (smtp, subject header)
263
     *   headers (smtp, array-hash of extra smtp headers)
264
     *
265
     * @return array of results
266
     * @access public
267
     */
268
    function &call($method, &$params, $namespace = false, $soapAction = false)
269
    {
270
        $this->headersIn = null;
271
        $this->__last_request = null;
272
        $this->__last_response = null;
273
        $this->wire = null;
274
        $this->xml = null;
275
 
276
        $soap_data =& $this->__generate($method, $params, $namespace, $soapAction);
277
        if (PEAR::isError($soap_data)) {
278
            return $this->_raiseSoapFault($soap_data);
279
        }
280
 
281
        // __generate may have changed the endpoint if the wsdl has more
282
        // than one service, so we need to see if we need to generate
283
        // a new transport to hook to a different URI.  Since the transport
284
        // protocol can also change, we need to get an entirely new object,
285
        // though this could probably be optimized.
286
        if (!$this->_soap_transport || $this->_endpoint != $this->_soap_transport->url) {
287
            $this->_soap_transport =& SOAP_Transport::getTransport($this->_endpoint);
288
            if (PEAR::isError($this->_soap_transport)) {
289
                $fault =& $this->_soap_transport;
290
                $this->_soap_transport = null;
291
                return $this->_raiseSoapFault($fault);
292
            }
293
        }
294
        $this->_soap_transport->encoding = $this->_encoding;
295
 
296
        // Send the message.
297
        $transport_options = array_merge_recursive($this->__proxy_params, $this->__options);
298
        $this->xml =& $this->_soap_transport->send($soap_data, $transport_options);
299
 
300
        // Save the wire information for debugging.
301
        if ($this->__options['trace'] > 0) {
302
            $this->__last_request =& $this->_soap_transport->outgoing_payload;
303
            $this->__last_response =& $this->_soap_transport->incoming_payload;
304
            $this->wire =& $this->__get_wire();
305
        }
306
        if ($this->_soap_transport->fault) {
307
            return $this->_raiseSoapFault($this->xml);
308
        }
309
 
310
        $this->__attachments =& $this->_soap_transport->attachments;
311
        $this->__result_encoding = $this->_soap_transport->result_encoding;
312
 
313
        if (isset($this->__options['result']) && $this->__options['result'] != 'parse') {
314
            return $this->xml;
315
        }
316
 
317
        return $this->__parse($this->xml, $this->__result_encoding, $this->__attachments);
318
    }
319
 
320
    /**
321
     * Sets option to use with the transports layers.
322
     *
323
     * An example of such use is
324
     * $soapclient->setOpt('curl', CURLOPT_VERBOSE, 1)
325
     * to pass a specific option to when using an SSL connection.
326
     *
327
     * @access public
328
     * @param  string  $category  category to which the option applies
329
     * @param  string  $option    option name
330
     * @param  string  $value     option value
331
     * @return void
332
     */
333
    function setOpt($category, $option, $value = null)
334
    {
335
        if (!is_null($value)) {
336
            if (!isset($this->__options[$category])) {
337
                $this->__options[$category] = array();
338
            }
339
            $this->__options[$category][$option] = $value;
340
        } else {
341
            $this->__options[$category] = $option;
342
        }
343
    }
344
 
345
    /**
346
     * Overload extension support
347
     * if the overload extension is loaded, you can call the client class
348
     * with a soap method name
349
     * $soap = new SOAP_Client(....);
350
     * $value = $soap->getStockQuote('MSFT');
351
     *
352
     * @param string method
353
     * @param array  args
354
     * @param string retur_value
355
     *
356
     * @return boolean
357
     * @access public
358
     */
359
    function _call($method, $args, &$return_value)
360
    {
361
        // XXX overloading lowercases the method name, we
362
        // need to look into the wsdl and try to find
363
        // the correct method name to get the correct
364
        // case for the call.
365
        if ($this->_wsdl) {
366
            $this->_wsdl->matchMethod($method);
367
        }
368
 
369
        $return_value =& $this->call($method, $args);
370
        return true;
371
    }
372
 
373
    function &__getlastrequest()
374
    {
375
        return $this->__last_request;
376
    }
377
 
378
    function &__getlastresponse()
379
    {
380
        return $this->__last_response;
381
    }
382
 
383
    function __use($use)
384
    {
385
        $this->__options['use'] = $use;
386
    }
387
 
388
    function __style($style)
389
    {
390
        $this->__options['style'] = $style;
391
    }
392
 
393
    function __trace($level)
394
    {
395
        $this->__options['trace'] = $level;
396
    }
397
 
398
    function &__generate($method, &$params, $namespace = false, $soapAction = false)
399
    {
400
        $this->fault = null;
401
        $this->__options['input']='parse';
402
        $this->__options['result']='parse';
403
        $this->__options['parameters'] = false;
404
        if ($params && gettype($params) != 'array') {
405
            $params = array($params);
406
        }
407
        if (gettype($namespace) == 'array') {
408
            foreach ($namespace as $optname => $opt) {
409
                $this->__options[strtolower($optname)] = $opt;
410
            }
411
            if (isset($this->__options['namespace'])) {
412
                $namespace = $this->__options['namespace'];
413
            } else {
414
                $namespace = false;
415
            }
416
        } else {
417
            // we'll place soapaction into our array for usage in the transport
418
            $this->__options['soapaction'] = $soapAction;
419
            $this->__options['namespace'] = $namespace;
420
        }
421
 
422
        if ($this->__endpointType == 'wsdl') {
423
            $this->_setSchemaVersion($this->_wsdl->xsd);
424
            // get portName
425
            if (!$this->_portName) {
426
                $this->_portName = $this->_wsdl->getPortName($method);
427
            }
428
            if (PEAR::isError($this->_portName)) {
429
                return $this->_raiseSoapFault($this->_portName);
430
            }
431
 
432
            // get endpoint
433
            $this->_endpoint = $this->_wsdl->getEndpoint($this->_portName);
434
            if (PEAR::isError($this->_endpoint)) {
435
                return $this->_raiseSoapFault($this->_endpoint);
436
            }
437
 
438
            // get operation data
439
            $opData = $this->_wsdl->getOperationData($this->_portName, $method);
440
 
441
            if (PEAR::isError($opData)) {
442
                return $this->_raiseSoapFault($opData);
443
            }
444
            $namespace = $opData['namespace'];
445
            $this->__options['style'] = $opData['style'];
446
            $this->__options['use'] = $opData['input']['use'];
447
            $this->__options['soapaction'] = $opData['soapAction'];
448
 
449
            // set input params
450
            if ($this->__options['input'] == 'parse') {
451
                $this->__options['parameters'] = $opData['parameters'];
452
                $nparams = array();
453
                if (isset($opData['input']['parts']) && count($opData['input']['parts']) > 0) {
454
                    $i = 0;
455
                    reset($params);
456
                    foreach ($opData['input']['parts'] as $name => $part) {
457
                        $xmlns = '';
458
                        $attrs = array();
459
                        // is the name actually a complex type?
460
                        if (isset($part['element'])) {
461
                            $xmlns = $this->_wsdl->namespaces[$part['namespace']];
462
                            $part = $this->_wsdl->elements[$part['namespace']][$part['type']];
463
                            $name = $part['name'];
464
                        }
465
                        if (array_key_exists($name, $params) ||
466
                            $this->_wsdl->getDataHandler($name, $part['namespace'])) {
467
                            $nparams[$name] =& $params[$name];
468
                        } else {
469
                            // we now force an associative array for
470
                            // parameters if using wsdl.
471
                            return $this->_raiseSoapFault("The named parameter $name is not in the call parameters.");
472
                        }
473
                        if (gettype($nparams[$name]) != 'object' ||
474
                            !is_a($nparams[$name],'soap_value')) {
475
                            // type is a qname likely, split it apart, and get the type namespace from wsdl
476
                            $qname =& new QName($part['type']);
477
                            if ($qname->ns) {
478
                                $type_namespace = $this->_wsdl->namespaces[$qname->ns];
479
                            } else if (isset($part['namespace'])) {
480
                                $type_namespace = $this->_wsdl->namespaces[$part['namespace']];
481
                            } else {
482
                                $type_namespace = null;
483
                            }
484
                            $qname->namespace = $type_namespace;
485
                            $type = $qname->name;
486
                            $pqname = $name;
487
                            if ($xmlns) {
488
                                $pqname = '{' . $xmlns . '}' . $name;
489
                            }
490
                            $nparams[$name] =& new SOAP_Value($pqname, $qname->fqn(), $nparams[$name], $attrs);
491
                        } else {
492
                            // wsdl fixups to the soap value.
493
                        }
494
                    }
495
                }
496
                $params =& $nparams;
497
                unset($nparams);
498
            }
499
        } else {
500
            $this->_setSchemaVersion(SOAP_XML_SCHEMA_VERSION);
501
        }
502
 
503
        // serialize the message.
504
        $this->_section5 = (isset($this->__options['use']) && $this->__options['use'] == 'literal');
505
 
506
        if (!isset($this->__options['style']) || $this->__options['style'] == 'rpc') {
507
            $this->__options['style'] = 'rpc';
508
            $this->docparams = true;
509
            $mqname =& new QName($method, $namespace);
510
            $methodValue =& new SOAP_Value($mqname->fqn(), 'Struct', $params);
511
            $soap_msg =& $this->_makeEnvelope($methodValue, $this->headersOut, $this->_encoding, $this->__options);
512
        } else {
513
            if (!$params) {
514
                $mqname =& new QName($method, $namespace);
515
                $mynull = null;
516
                $params =& new SOAP_Value($mqname->fqn(), 'Struct', $mynull);
517
            } elseif ($this->__options['input'] == 'parse') {
518
                if (is_array($params)) {
519
                    $nparams = array();
520
                    $keys = array_keys($params);
521
                    foreach ($keys as $k) {
522
                        if (gettype($params[$k]) != 'object') {
523
                            $nparams[] =& new SOAP_Value($k, false, $params[$k]);
524
                        } else {
525
                            $nparams[] =& $params[$k];
526
                        }
527
                    }
528
                    $params =& $nparams;
529
                }
530
                if ($this->__options['parameters']) {
531
                    $mqname =& new QName($method, $namespace);
532
                    $params =& new SOAP_Value($mqname->fqn(), 'Struct', $params);
533
                }
534
            }
535
            $soap_msg =& $this->_makeEnvelope($params, $this->headersOut, $this->_encoding, $this->__options);
536
        }
537
        unset($this->headersOut);
538
 
539
        if (PEAR::isError($soap_msg)) {
540
            return $this->_raiseSoapFault($soap_msg);
541
        }
542
 
543
        // handle Mime or DIME encoding
544
        // XXX DIME Encoding should move to the transport, do it here for now
545
        // and for ease of getting it done
546
        if (count($this->__attachments)) {
547
            if ((isset($this->__options['attachments']) && $this->__options['attachments'] == 'Mime') || isset($this->__options['Mime'])) {
548
                $soap_msg =& $this->_makeMimeMessage($soap_msg, $this->_encoding);
549
            } else {
550
                // default is dime
551
                $soap_msg =& $this->_makeDIMEMessage($soap_msg, $this->_encoding);
552
                $this->__options['headers']['Content-Type'] = 'application/dime';
553
            }
554
            if (PEAR::isError($soap_msg)) {
555
                return $this->_raiseSoapFault($soap_msg);
556
            }
557
        }
558
 
559
        // instantiate client
560
        if (is_array($soap_msg)) {
561
            $soap_data =& $soap_msg['body'];
562
            if (count($soap_msg['headers'])) {
563
                if (isset($this->__options['headers'])) {
564
                    $this->__options['headers'] = array_merge($this->__options['headers'], $soap_msg['headers']);
565
                } else {
566
                    $this->__options['headers'] = $soap_msg['headers'];
567
                }
568
            }
569
        } else {
570
            $soap_data =& $soap_msg;
571
        }
572
        return $soap_data;
573
    }
574
 
575
    function &__parse(&$response, $encoding, &$attachments)
576
    {
577
        // parse the response
578
        $response =& new SOAP_Parser($response, $encoding, $attachments);
579
        if ($response->fault) {
580
            return $this->_raiseSoapFault($response->fault);
581
        }
582
        // return array of parameters
583
        $return =& $response->getResponse();
584
        $headers =& $response->getHeaders();
585
        if ($headers) {
586
            $this->headersIn =& $this->__decodeResponse($headers, false);
587
        }
588
        return $this->__decodeResponse($return);
589
    }
590
 
591
    function &__decodeResponse(&$response, $shift = true)
592
    {
593
        if (!$response) {
594
            return null;
595
        }
596
        // Check for valid response.
597
        if (PEAR::isError($response)) {
598
            return $this->_raiseSoapFault($response);
599
        } elseif (!is_a($response, 'soap_value')) {
600
            return $this->_raiseSoapFault("didn't get SOAP_Value object back from client");
601
        }
602
 
603
        // Decode to native php datatype.
604
        $returnArray =& $this->_decode($response);
605
 
606
        // Fault?
607
        if (PEAR::isError($returnArray)) {
608
            return $this->_raiseSoapFault($returnArray);
609
        }
610
        if (is_object($returnArray) && strcasecmp(get_class($returnArray),'stdClass') == 0) {
611
            $returnArray = get_object_vars($returnArray);
612
        }
613
        if (is_array($returnArray)) {
614
            if (isset($returnArray['faultcode']) || isset($returnArray['SOAP-ENV:faultcode'])) {
615
                $faultcode = $faultstring = $faultdetail = $faultactor = '';
616
                foreach ($returnArray as $k => $v) {
617
                    if (stristr($k, 'faultcode')) $faultcode = $v;
618
                    if (stristr($k, 'faultstring')) $faultstring = $v;
619
                    if (stristr($k, 'detail')) $faultdetail = $v;
620
                    if (stristr($k, 'faultactor')) $faultactor = $v;
621
                }
622
                return $this->_raiseSoapFault($faultstring, $faultdetail, $faultactor, $faultcode);
623
            }
624
            // Return array of return values.
625
            if ($shift && count($returnArray) == 1) {
626
                return array_shift($returnArray);
627
            }
628
            return $returnArray;
629
        }
630
        return $returnArray;
631
    }
632
 
633
    function __get_wire()
634
    {
635
        if ($this->__options['trace'] > 0 && ($this->__last_request || $this->__last_response)) {
636
            return "OUTGOING:\n\n".
637
            $this->__last_request.
638
            "\n\nINCOMING\n\n".
639
            preg_replace("/></",">\r\n<", $this->__last_response);
640
        }
641
        return null;
642
    }
643
 
644
}