class.soapclient.php 31 KB

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