7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Client.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /** Zend_Soap_Server */
24 require_once 'Zend/Soap/Server.php';
26 /** Zend_Soap_Client_Local */
27 require_once 'Zend/Soap/Client/Local.php';
29 /** Zend_Soap_Client_Common */
30 require_once 'Zend/Soap/Client/Common.php';
38 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
41 class Zend_Soap_Client
47 protected $_encoding = 'UTF-8';
50 * Array of SOAP type => PHP class pairings for handling return/incoming values
53 protected $_classmap = null;
56 * Registered fault exceptions
59 protected $_faultExceptions = array();
62 * SOAP version to use; SOAP_1_2 by default, to allow processing of headers
65 protected $_soapVersion = SOAP_1_2
;
67 /** Set of other SoapClient options */
68 protected $_uri = null;
69 protected $_location = null;
70 protected $_style = null;
71 protected $_use = null;
72 protected $_login = null;
73 protected $_password = null;
74 protected $_proxy_host = null;
75 protected $_proxy_port = null;
76 protected $_proxy_login = null;
77 protected $_proxy_password = null;
78 protected $_local_cert = null;
79 protected $_passphrase = null;
80 protected $_compression = null;
81 protected $_connection_timeout = null;
82 protected $_stream_context = null;
83 protected $_features = null;
84 protected $_cache_wsdl = null;
87 * WSDL used to access server
88 * It also defines Zend_Soap_Client working mode (WSDL vs non-WSDL)
92 protected $_wsdl = null;
99 protected $_soapClient;
102 * Last invoked method
106 protected $_lastMethod = '';
109 * SOAP request headers.
111 * Array of SoapHeader objects
115 protected $_soapInputHeaders = array();
118 * Permanent SOAP request headers (shared between requests).
120 * Array of SoapHeader objects
124 protected $_permanentSoapInputHeaders = array();
127 * Output SOAP headers.
129 * Array of SoapHeader objects
133 protected $_soapOutputHeaders = array();
138 * @param string $wsdl
139 * @param array $options
141 public function __construct($wsdl = null, $options = null)
143 if (!extension_loaded('soap')) {
144 require_once 'Zend/Soap/Client/Exception.php';
145 throw new Zend_Soap_Client_Exception('SOAP extension is not loaded.');
148 if ($wsdl !== null) {
149 $this->setWsdl($wsdl);
151 if ($options !== null) {
152 $this->setOptions($options);
159 * @param string $wsdl
160 * @return Zend_Soap_Client
162 public function setWsdl($wsdl)
164 $this->_wsdl
= $wsdl;
165 $this->_soapClient
= null;
175 public function getWsdl()
183 * Allows setting options as an associative array of option => value pairs.
185 * @param array|Zend_Config $options
186 * @return Zend_Soap_Client
187 * @throws Zend_SoapClient_Exception
189 public function setOptions($options)
191 if($options instanceof Zend_Config
) {
192 $options = $options->toArray();
195 foreach ($options as $key => $value) {
199 $this->setClassmap($value);
202 $this->setEncoding($value);
206 $this->setSoapVersion($value);
209 $this->setWsdl($value);
212 $this->setUri($value);
215 $this->setLocation($value);
218 $this->setStyle($value);
221 $this->setEncodingMethod($value);
224 $this->setHttpLogin($value);
227 $this->setHttpPassword($value);
230 $this->setProxyHost($value);
233 $this->setProxyPort($value);
236 $this->setProxyLogin($value);
238 case 'proxy_password':
239 $this->setProxyPassword($value);
242 $this->setHttpsCertificate($value);
245 $this->setHttpsCertPassphrase($value);
248 $this->setCompressionOptions($value);
250 case 'stream_context':
251 $this->setStreamContext($value);
254 $this->setSoapFeatures($value);
257 $this->setWsdlCache($value);
261 // case 'connection_timeout':
262 // $this->_connection_timeout = $value;
266 require_once 'Zend/Soap/Client/Exception.php';
267 throw new Zend_Soap_Client_Exception('Unknown SOAP client option');
276 * Return array of options suitable for using with SoapClient constructor
280 public function getOptions()
284 $options['classmap'] = $this->getClassmap();
285 $options['encoding'] = $this->getEncoding();
286 $options['soap_version'] = $this->getSoapVersion();
287 $options['wsdl'] = $this->getWsdl();
288 $options['uri'] = $this->getUri();
289 $options['location'] = $this->getLocation();
290 $options['style'] = $this->getStyle();
291 $options['use'] = $this->getEncodingMethod();
292 $options['login'] = $this->getHttpLogin();
293 $options['password'] = $this->getHttpPassword();
294 $options['proxy_host'] = $this->getProxyHost();
295 $options['proxy_port'] = $this->getProxyPort();
296 $options['proxy_login'] = $this->getProxyLogin();
297 $options['proxy_password'] = $this->getProxyPassword();
298 $options['local_cert'] = $this->getHttpsCertificate();
299 $options['passphrase'] = $this->getHttpsCertPassphrase();
300 $options['compression'] = $this->getCompressionOptions();
301 //$options['connection_timeout'] = $this->_connection_timeout;
302 $options['stream_context'] = $this->getStreamContext();
303 $options['cache_wsdl'] = $this->getWsdlCache();
304 $options['features'] = $this->getSoapFeatures();
306 foreach ($options as $key => $value) {
307 if ($value == null) {
308 unset($options[$key]);
318 * @param int $version One of the SOAP_1_1 or SOAP_1_2 constants
319 * @return Zend_Soap_Client
320 * @throws Zend_Soap_Client_Exception with invalid soap version argument
322 public function setSoapVersion($version)
324 if (!in_array($version, array(SOAP_1_1
, SOAP_1_2
))) {
325 require_once 'Zend/Soap/Client/Exception.php';
326 throw new Zend_Soap_Client_Exception('Invalid soap version specified. Use SOAP_1_1 or SOAP_1_2 constants.');
328 $this->_soapVersion
= $version;
330 $this->_soapClient
= null;
340 public function getSoapVersion()
342 return $this->_soapVersion
;
348 * @param array $classmap
349 * @return Zend_Soap_Client
350 * @throws Zend_Soap_Client_Exception for any invalid class in the class map
352 public function setClassmap(array $classmap)
354 foreach ($classmap as $type => $class) {
355 if (!class_exists($class)) {
356 require_once 'Zend/Soap/Client/Exception.php';
357 throw new Zend_Soap_Client_Exception('Invalid class in class map');
361 $this->_classmap
= $classmap;
363 $this->_soapClient
= null;
373 public function getClassmap()
375 return $this->_classmap
;
381 * @param string $encoding
382 * @return Zend_Soap_Client
383 * @throws Zend_Soap_Client_Exception with invalid encoding argument
385 public function setEncoding($encoding)
387 if (!is_string($encoding)) {
388 require_once 'Zend/Soap/Client/Exception.php';
389 throw new Zend_Soap_Client_Exception('Invalid encoding specified');
392 $this->_encoding
= $encoding;
394 $this->_soapClient
= null;
404 public function getEncoding()
406 return $this->_encoding
;
410 * Check for valid URN
414 * @throws Zend_Soap_Client_Exception on invalid URN
416 public function validateUrn($urn)
418 $segs = parse_url($urn);
419 if (isset($segs['scheme'])) {
423 require_once 'Zend/Soap/Client/Exception.php';
424 throw new Zend_Soap_Client_Exception('Invalid URN');
430 * URI in Web Service the target namespace
433 * @return Zend_Soap_Client
434 * @throws Zend_Soap_Client_Exception with invalid uri argument
436 public function setUri($uri)
438 $this->validateUrn($uri);
441 $this->_soapClient
= null;
451 public function getUri()
459 * URI in Web Service the target namespace
461 * @param string $location
462 * @return Zend_Soap_Client
463 * @throws Zend_Soap_Client_Exception with invalid uri argument
465 public function setLocation($location)
467 $this->validateUrn($location);
468 $this->_location
= $location;
470 $this->_soapClient
= null;
480 public function getLocation()
482 return $this->_location
;
488 * @param int $style One of the SOAP_RPC or SOAP_DOCUMENT constants
489 * @return Zend_Soap_Client
490 * @throws Zend_Soap_Client_Exception with invalid style argument
492 public function setStyle($style)
494 if (!in_array($style, array(SOAP_RPC
, SOAP_DOCUMENT
))) {
495 require_once 'Zend/Soap/Client/Exception.php';
496 throw new Zend_Soap_Client_Exception('Invalid request style specified. Use SOAP_RPC or SOAP_DOCUMENT constants.');
499 $this->_style
= $style;
501 $this->_soapClient
= null;
511 public function getStyle()
513 return $this->_style
;
517 * Set message encoding method
519 * @param int $use One of the SOAP_ENCODED or SOAP_LITERAL constants
520 * @return Zend_Soap_Client
521 * @throws Zend_Soap_Client_Exception with invalid message encoding method argument
523 public function setEncodingMethod($use)
525 if (!in_array($use, array(SOAP_ENCODED
, SOAP_LITERAL
))) {
526 require_once 'Zend/Soap/Client/Exception.php';
527 throw new Zend_Soap_Client_Exception('Invalid message encoding method. Use SOAP_ENCODED or SOAP_LITERAL constants.');
532 $this->_soapClient
= null;
538 * Get message encoding method
542 public function getEncodingMethod()
550 * @param string $login
551 * @return Zend_Soap_Client
553 public function setHttpLogin($login)
555 $this->_login
= $login;
557 $this->_soapClient
= null;
563 * Retrieve HTTP Login
567 public function getHttpLogin()
569 return $this->_login
;
575 * @param string $password
576 * @return Zend_Soap_Client
578 public function setHttpPassword($password)
580 $this->_password
= $password;
582 $this->_soapClient
= null;
588 * Retrieve HTTP Password
592 public function getHttpPassword()
594 return $this->_password
;
600 * @param string $proxyHost
601 * @return Zend_Soap_Client
603 public function setProxyHost($proxyHost)
605 $this->_proxy_host
= $proxyHost;
607 $this->_soapClient
= null;
613 * Retrieve proxy host
617 public function getProxyHost()
619 return $this->_proxy_host
;
625 * @param int $proxyPort
626 * @return Zend_Soap_Client
628 public function setProxyPort($proxyPort)
630 $this->_proxy_port
= (int)$proxyPort;
632 $this->_soapClient
= null;
638 * Retrieve proxy port
642 public function getProxyPort()
644 return $this->_proxy_port
;
650 * @param string $proxyLogin
651 * @return Zend_Soap_Client
653 public function setProxyLogin($proxyLogin)
655 $this->_proxy_login
= $proxyLogin;
657 $this->_soapClient
= null;
663 * Retrieve proxy login
667 public function getProxyLogin()
669 return $this->_proxy_login
;
675 * @param string $proxyLogin
676 * @return Zend_Soap_Client
678 public function setProxyPassword($proxyPassword)
680 $this->_proxy_password
= $proxyPassword;
682 $this->_soapClient
= null;
688 * Set HTTPS client certificate path
690 * @param string $localCert local certificate path
691 * @return Zend_Soap_Client
692 * @throws Zend_Soap_Client_Exception with invalid local certificate path argument
694 public function setHttpsCertificate($localCert)
696 if (!is_readable($localCert)) {
697 require_once 'Zend/Soap/Client/Exception.php';
698 throw new Zend_Soap_Client_Exception('Invalid HTTPS client certificate path.');
701 $this->_local_cert
= $localCert;
703 $this->_soapClient
= null;
709 * Get HTTPS client certificate path
713 public function getHttpsCertificate()
715 return $this->_local_cert
;
719 * Set HTTPS client certificate passphrase
721 * @param string $passphrase
722 * @return Zend_Soap_Client
724 public function setHttpsCertPassphrase($passphrase)
726 $this->_passphrase
= $passphrase;
728 $this->_soapClient
= null;
734 * Get HTTPS client certificate passphrase
738 public function getHttpsCertPassphrase()
740 return $this->_passphrase
;
744 * Set compression options
746 * @param int $compressionOptions
747 * @return Zend_Soap_Client
749 public function setCompressionOptions($compressionOptions)
751 $this->_compression
= $compressionOptions;
753 $this->_soapClient
= null;
759 * Get Compression options
763 public function getCompressionOptions()
765 return $this->_compression
;
769 * Retrieve proxy password
773 public function getProxyPassword()
775 return $this->_proxy_password
;
781 * @return Zend_Soap_Client
783 public function setStreamContext($context)
785 if(!is_resource($context) ||
get_resource_type($context) !== "stream-context") {
787 * @see Zend_Soap_Client_Exception
789 require_once "Zend/Soap/Client/Exception.php";
790 throw new Zend_Soap_Client_Exception(
791 "Invalid stream context resource given."
795 $this->_stream_context
= $context;
804 public function getStreamContext()
806 return $this->_stream_context
;
810 * Set the SOAP Feature options.
812 * @param string|int $feature
813 * @return Zend_Soap_Client
815 public function setSoapFeatures($feature)
817 $this->_features
= $feature;
819 $this->_soapClient
= null;
824 * Return current SOAP Features options
828 public function getSoapFeatures()
830 return $this->_features
;
834 * Set the SOAP Wsdl Caching Options
836 * @param string|int|boolean $caching
837 * @return Zend_Soap_Client
839 public function setWsdlCache($options)
841 $this->_cache_wsdl
= $options;
846 * Get current SOAP Wsdl Caching option
848 public function getWsdlCache()
850 return $this->_cache_wsdl
;
854 * Retrieve request XML
858 public function getLastRequest()
860 if ($this->_soapClient
!== null) {
861 return $this->_soapClient
->__getLastRequest();
872 public function getLastResponse()
874 if ($this->_soapClient
!== null) {
875 return $this->_soapClient
->__getLastResponse();
882 * Retrieve request headers
886 public function getLastRequestHeaders()
888 if ($this->_soapClient
!== null) {
889 return $this->_soapClient
->__getLastRequestHeaders();
896 * Retrieve response headers (as string)
900 public function getLastResponseHeaders()
902 if ($this->_soapClient
!== null) {
903 return $this->_soapClient
->__getLastResponseHeaders();
910 * Retrieve last invoked method
914 public function getLastMethod()
916 return $this->_lastMethod
;
920 * Do request proxy method.
922 * May be overridden in subclasses
925 * @param Zend_Soap_Client_Common $client
926 * @param string $request
927 * @param string $location
928 * @param string $action
929 * @param int $version
930 * @param int $one_way
933 public function _doRequest(Zend_Soap_Client_Common
$client, $request, $location, $action, $version, $one_way = null)
935 // Perform request as is
936 if ($one_way == null) {
937 return call_user_func(array($client,'SoapClient::__doRequest'), $request, $location, $action, $version);
939 return call_user_func(array($client,'SoapClient::__doRequest'), $request, $location, $action, $version, $one_way);
944 * Initialize SOAP Client object
946 * @throws Zend_Soap_Client_Exception
948 protected function _initSoapClientObject()
950 $wsdl = $this->getWsdl();
951 $options = array_merge($this->getOptions(), array('trace' => true));
954 if (!isset($options['location'])) {
955 require_once 'Zend/Soap/Client/Exception.php';
956 throw new Zend_Soap_Client_Exception('\'location\' parameter is required in non-WSDL mode.');
958 if (!isset($options['uri'])) {
959 require_once 'Zend/Soap/Client/Exception.php';
960 throw new Zend_Soap_Client_Exception('\'uri\' parameter is required in non-WSDL mode.');
963 if (isset($options['use'])) {
964 require_once 'Zend/Soap/Client/Exception.php';
965 throw new Zend_Soap_Client_Exception('\'use\' parameter only works in non-WSDL mode.');
967 if (isset($options['style'])) {
968 require_once 'Zend/Soap/Client/Exception.php';
969 throw new Zend_Soap_Client_Exception('\'style\' parameter only works in non-WSDL mode.');
972 unset($options['wsdl']);
974 $this->_soapClient
= new Zend_Soap_Client_Common(array($this, '_doRequest'), $wsdl, $options);
979 * Perform arguments pre-processing
981 * My be overridden in descendant classes
983 * @param array $arguments
985 protected function _preProcessArguments($arguments)
992 * Perform result pre-processing
994 * My be overridden in descendant classes
996 * @param array $arguments
998 protected function _preProcessResult($result)
1005 * Add SOAP input header
1007 * @param SoapHeader $header
1008 * @param boolean $permanent
1009 * @return Zend_Soap_Client
1011 public function addSoapInputHeader(SoapHeader
$header, $permanent = false)
1014 $this->_permanentSoapInputHeaders
[] = $header;
1016 $this->_soapInputHeaders
[] = $header;
1023 * Reset SOAP input headers
1025 * @return Zend_Soap_Client
1027 public function resetSoapInputHeaders()
1029 $this->_permanentSoapInputHeaders
= array();
1030 $this->_soapInputHeaders
= array();
1036 * Get last SOAP output headers
1040 public function getLastSoapOutputHeaderObjects()
1042 return $this->_soapOutputHeaders
;
1046 * Perform a SOAP call
1048 * @param string $name
1049 * @param array $arguments
1052 public function __call($name, $arguments)
1054 $soapClient = $this->getSoapClient();
1056 $this->_lastMethod
= $name;
1058 $soapHeaders = array_merge($this->_permanentSoapInputHeaders
, $this->_soapInputHeaders
);
1059 $result = $soapClient->__soapCall($name,
1060 $this->_preProcessArguments($arguments),
1061 null, /* Options are already set to the SOAP client object */
1062 (count($soapHeaders) > 0)?
$soapHeaders : null,
1063 $this->_soapOutputHeaders
);
1065 // Reset non-permanent input headers
1066 $this->_soapInputHeaders
= array();
1068 return $this->_preProcessResult($result);
1073 * Return a list of available functions
1076 * @throws Zend_Soap_Client_Exception
1078 public function getFunctions()
1080 if ($this->getWsdl() == null) {
1081 require_once 'Zend/Soap/Client/Exception.php';
1082 throw new Zend_Soap_Client_Exception('\'getFunctions\' method is available only in WSDL mode.');
1085 $soapClient = $this->getSoapClient();
1086 return $soapClient->__getFunctions();
1097 * Return a list of SOAP types
1100 * @throws Zend_Soap_Client_Exception
1102 public function getTypes()
1104 if ($this->getWsdl() == null) {
1105 require_once 'Zend/Soap/Client/Exception.php';
1106 throw new Zend_Soap_Client_Exception('\'getTypes\' method is available only in WSDL mode.');
1109 $soapClient = $this->getSoapClient();
1111 return $soapClient->__getTypes();
1115 * @param SoapClient $soapClient
1116 * @return Zend_Soap_Client
1118 public function setSoapClient(SoapClient
$soapClient)
1120 $this->_soapClient
= $soapClient;
1125 * @return SoapClient
1127 public function getSoapClient()
1129 if ($this->_soapClient
== null) {
1130 $this->_initSoapClientObject();
1132 return $this->_soapClient
;
1136 * @param string $name
1137 * @param string $value
1138 * @return Zend_Soap_Client
1140 public function setCookie($cookieName, $cookieValue=null)
1142 $soapClient = $this->getSoapClient();
1143 $soapClient->__setCookie($cookieName, $cookieValue);