class.soapclient.php 33 KB

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