• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

webservice/soap/lib/class.soapclient.php

Go to the documentation of this file.
00001 <?php
00002 
00003 
00004 
00005 
00025 class soap_client extends nusoap_base  {
00026 
00027         var $username = '';
00028         var $password = '';
00029         var $authtype = '';
00030         var $requestHeaders = false;    // SOAP headers in request (text)
00031         var $responseHeaders = '';              // SOAP headers from response (incomplete namespace resolution) (text)
00032         var $document = '';                             // SOAP body response portion (incomplete namespace resolution) (text)
00033         var $endpoint;
00034         var $error_str = false;
00035     var $proxyhost = '';
00036     var $proxyport = '';
00037         var $proxyusername = '';
00038         var $proxypassword = '';
00039     var $xml_encoding = '';                     // character set encoding of incoming (response) messages
00040         var $http_encoding = false;
00041         var $timeout = 0;                               // HTTP connection timeout
00042         var $response_timeout = 30;             // HTTP response timeout
00043         var $endpointType = '';
00044         var $persistentConnection = false;
00045         var $defaultRpcParams = false;  // This is no longer used
00046         var $request = '';                              // HTTP request
00047         var $response = '';                             // HTTP response
00048         var $responseData = '';                 // SOAP payload of response
00049         // toggles whether the parser decodes element content w/ utf8_decode()
00050     var $decode_utf8 = true;
00051         
00061         var $fault, $faultcode, $faultstring, $faultdetail;
00062 
00077         function soap_client($endpoint,$wsdl = false,$proxyhost = false,$proxyport = false,$proxyusername = false, $proxypassword = false, $timeout = 0, $response_timeout = 30){
00078                 $this->endpoint = $endpoint;
00079                 $this->proxyhost = $proxyhost;
00080                 $this->proxyport = $proxyport;
00081                 $this->proxyusername = $proxyusername;
00082                 $this->proxypassword = $proxypassword;
00083                 $this->timeout = $timeout;
00084                 $this->response_timeout = $response_timeout;
00085 
00086                 // make values
00087                 if($wsdl){
00088                         $this->endpointType = 'wsdl';
00089                         if (is_object($endpoint) && is_a($endpoint, 'wsdl')) {
00090                                 $this->wsdl = $endpoint;
00091                                 $this->endpoint = $this->wsdl->wsdl;
00092                                 $this->wsdlFile = $this->endpoint;
00093                                 $this->debug('existing wsdl instance created from ' . $this->endpoint);
00094                         } else {
00095                                 $this->wsdlFile = $this->endpoint;
00096                                 
00097                                 // instantiate wsdl object and parse wsdl file
00098                                 $this->debug('instantiating wsdl class with doc: '.$endpoint);
00099                                 $this->wsdl =& new wsdl($this->wsdlFile,$this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword,$this->timeout,$this->response_timeout);
00100                         }
00101                         $this->debug("wsdl debug...\n".$this->wsdl->debug_str);
00102                         $this->wsdl->debug_str = '';
00103                         // catch errors
00104                         if($errstr = $this->wsdl->getError()){
00105                                 $this->debug('got wsdl error: '.$errstr);
00106                                 $this->setError('wsdl error: '.$errstr);
00107                         } elseif($this->operations = $this->wsdl->getOperations()){
00108                                 $this->debug( 'got '.count($this->operations).' operations from wsdl '.$this->wsdlFile);
00109                         } else {
00110                                 $this->debug( 'getOperations returned false');
00111                                 $this->setError('no operations defined in the WSDL document!');
00112                         }
00113                 }
00114         }
00115 
00141         function call($operation,$params=array(),$namespace='',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){
00142                 $this->operation = $operation;
00143                 $this->fault = false;
00144                 $this->error_str = '';
00145                 $this->request = '';
00146                 $this->response = '';
00147                 $this->responseData = '';
00148                 $this->faultstring = '';
00149                 $this->faultcode = '';
00150                 $this->opData = array();
00151                 
00152                 $this->debug("call: $operation, $params, $namespace, $soapAction, $headers, $style, $use; endpointType: $this->endpointType");
00153                 if ($headers) {
00154                         $this->requestHeaders = $headers;
00155                 }
00156                 // serialize parameters
00157                 if($this->endpointType == 'wsdl' && $opData = $this->getOperationData($operation)){
00158                         // use WSDL for operation
00159                         $this->opData = $opData;
00160                         foreach($opData as $key => $value){
00161                                 $this->debug("$key -> $value");
00162                         }
00163                         if (isset($opData['soapAction'])) {
00164                                 $soapAction = $opData['soapAction'];
00165                         }
00166                         $this->endpoint = $opData['endpoint'];
00167                         $namespace = isset($opData['input']['namespace']) ? $opData['input']['namespace'] :     ($namespace != '' ? $namespace : 'http://testuri.org');
00168                         $style = $opData['style'];
00169                         $use = $opData['input']['use'];
00170                         // add ns to ns array
00171                         if($namespace != '' && !isset($this->wsdl->namespaces[$namespace])){
00172                                 $this->wsdl->namespaces['nu'] = $namespace;
00173             }
00174             $nsPrefix = $this->wsdl->getPrefixFromNamespace($namespace);
00175                         // serialize payload
00176                         if (is_string($params)) {
00177                                 $this->debug("serializing param string for WSDL operation $operation");
00178                                 $payload = $params;
00179                         } elseif (is_array($params)) {
00180                                 $this->debug("serializing param array for WSDL operation $operation");
00181                                 $payload = $this->wsdl->serializeRPCParameters($operation,'input',$params);
00182                         } else {
00183                                 $this->debug('params must be array or string');
00184                                 $this->setError('params must be array or string');
00185                                 return false;
00186                         }
00187             $usedNamespaces = $this->wsdl->usedNamespaces;
00188                         // Partial fix for multiple encoding styles in the same function call
00189                         $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
00190                         if (isset($opData['output']['encodingStyle']) && $encodingStyle != $opData['output']['encodingStyle']) {
00191                                 $methodEncodingStyle = ' SOAP-ENV:encodingStyle="' . $opData['output']['encodingStyle'] . '"';
00192                         } else {
00193                                 $methodEncodingStyle = '';
00194                         }
00195                         $this->debug("wsdl debug: \n".$this->wsdl->debug_str);
00196                         $this->wsdl->debug_str = '';
00197                         if ($errstr = $this->wsdl->getError()) {
00198                                 $this->debug('got wsdl error: '.$errstr);
00199                                 $this->setError('wsdl error: '.$errstr);
00200                                 return false;
00201                         }
00202                 } elseif($this->endpointType == 'wsdl') {
00203                         // operation not in WSDL
00204                         $this->setError( 'operation '.$operation.' not present.');
00205                         $this->debug("operation '$operation' not present.");
00206                         $this->debug("wsdl debug: \n".$this->wsdl->debug_str);
00207                         $this->wsdl->debug_str = '';
00208                         return false;
00209                 } else {
00210                         // no WSDL
00211             if($namespace == ''){
00212                 $namespace = 'http://testuri.org';
00213             }
00214                         //$this->namespaces['ns1'] = $namespace;
00215                         $nsPrefix = 'ns1';
00216                         // serialize 
00217                         $payload = '';
00218                         if (is_string($params)) {
00219                                 $this->debug("serializing param string for operation $operation");
00220                                 $payload = $params;
00221                         } elseif (is_array($params)) {
00222                                 $this->debug("serializing param array for operation $operation");
00223                                 foreach($params as $k => $v){
00224                                         $payload .= $this->serialize_val($v,$k,false,false,false,false,$use);
00225                                 }
00226                         } else {
00227                                 $this->debug('params must be array or string');
00228                                 $this->setError('params must be array or string');
00229                                 return false;
00230                         }
00231                         $usedNamespaces = array();
00232                         $methodEncodingStyle = '';
00233                 }
00234                 // wrap RPC calls with method element
00235                 if ($style == 'rpc') {
00236                         if ($use == 'literal') {
00237                                 $this->debug("wrapping RPC request with literal method element");
00238                                 $payload = "<$operation xmlns=\"$namespace\">" . $payload . "</$operation>";
00239                         } else {
00240                                 $this->debug("wrapping RPC request with encoded method element");
00241                                 $payload = "<$nsPrefix:$operation$methodEncodingStyle xmlns:$nsPrefix=\"$namespace\">" .
00242                                                         $payload .
00243                                                         "</$nsPrefix:$operation>";
00244                         }
00245                 }
00246                 // serialize envelope
00247                 $soapmsg = $this->serializeEnvelope($payload,$this->requestHeaders,$usedNamespaces,$style,$use);
00248                 $this->debug("endpoint: $this->endpoint, soapAction: $soapAction, namespace: $namespace, style: $style, use: $use");
00249                 $this->debug('SOAP message length: ' . strlen($soapmsg) . ' contents: ' . substr($soapmsg, 0, 1000));
00250                 // send
00251                 $return = $this->send($this->getHTTPBody($soapmsg),$soapAction,$this->timeout,$this->response_timeout);
00252                 if($errstr = $this->getError()){
00253                         $this->debug('Error: '.$errstr);
00254                         return false;
00255                 } else {
00256                         $this->return = $return;
00257                         $this->debug('sent message successfully and got a(n) '.gettype($return).' back');
00258                         
00259                         // fault?
00260                         if(is_array($return) && isset($return['faultcode'])){
00261                                 $this->debug('got fault');
00262                                 $this->setError($return['faultcode'].': '.$return['faultstring']);
00263                                 $this->fault = true;
00264                                 foreach($return as $k => $v){
00265                                         $this->$k = $v;
00266                                         $this->debug("$k = $v<br>");
00267                                 }
00268                                 return $return;
00269                         } else {
00270                                 // array of return values
00271                                 if(is_array($return)){
00272                                         // multiple 'out' parameters
00273                                         if(sizeof($return) > 1){
00274                                                 return $return;
00275                                         }
00276                                         // single 'out' parameter
00277                                         return array_shift($return);
00278                                 // nothing returned (ie, echoVoid)
00279                                 } else {
00280                                         return "";
00281                                 }
00282                         }
00283                 }
00284         }
00285 
00293         function getOperationData($operation){
00294                 if(isset($this->operations[$operation])){
00295                         return $this->operations[$operation];
00296                 }
00297                 $this->debug("No data for operation: $operation");
00298         }
00299 
00314         function send($msg, $soapaction = '', $timeout=0, $response_timeout=30) {
00315                 // detect transport
00316                 switch(true){
00317                         // http(s)
00318                         case ereg('^http',$this->endpoint):
00319                                 $this->debug('transporting via HTTP');
00320                                 if($this->persistentConnection == true && is_object($this->persistentConnection)){
00321                                         $http =& $this->persistentConnection;
00322                                 } else {
00323                                         $http = new soap_transport_http($this->endpoint);
00324                                         if ($this->persistentConnection) {
00325                                                 $http->usePersistentConnection();
00326                                         }
00327                                 }
00328                                 $http->setContentType($this->getHTTPContentType(), $this->getHTTPContentTypeCharset());
00329                                 $http->setSOAPAction($soapaction);
00330                                 if($this->proxyhost && $this->proxyport){
00331                                         $http->setProxy($this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword);
00332                                 }
00333                 if($this->username != '' && $this->password != '') {
00334                                         $http->setCredentials($this->username, $this->password, $this->authtype);
00335                                 }
00336                                 if($this->http_encoding != ''){
00337                                         $http->setEncoding($this->http_encoding);
00338                                 }
00339                                 $this->debug('sending message, length: '.strlen($msg));
00340                                 if(ereg('^http:',$this->endpoint)){
00341                                 //if(strpos($this->endpoint,'http:')){
00342                                         $this->responseData = $http->send($msg,$timeout,$response_timeout);
00343                                 } elseif(ereg('^https',$this->endpoint)){
00344                                 //} elseif(strpos($this->endpoint,'https:')){
00345                                         //if(phpversion() == '4.3.0-dev'){
00346                                                 //$response = $http->send($msg,$timeout,$response_timeout);
00347                                 //$this->request = $http->outgoing_payload;
00348                                                 //$this->response = $http->incoming_payload;
00349                                         //} else
00350                                         if (extension_loaded('curl')) {
00351                                                 $this->responseData = $http->sendHTTPS($msg,$timeout,$response_timeout);
00352                                         } else {
00353                                                 $this->setError('CURL Extension, or OpenSSL extension w/ PHP version >= 4.3 is required for HTTPS');
00354                                         }                                                               
00355                                 } else {
00356                                         $this->setError('no http/s in endpoint url');
00357                                 }
00358                                 $this->request = $http->outgoing_payload;
00359                                 $this->response = $http->incoming_payload;
00360                                 $this->debug("transport debug data...\n".$http->debug_str);
00361                                 
00362                                 // save transport object if using persistent connections
00363                                 if ($this->persistentConnection) {
00364                                         $http->debug_str = '';
00365                                         if (!is_object($this->persistentConnection)) {
00366                                                 $this->persistentConnection = $http;
00367                                         }
00368                                 }
00369                                 
00370                                 if($err = $http->getError()){
00371                                         $this->setError('HTTP Error: '.$err);
00372                                         return false;
00373                                 } elseif($this->getError()){
00374                                         return false;
00375                                 } else {
00376                                         $this->debug('got response, length: '. strlen($this->responseData).' type: '.$http->incoming_headers['content-type']);
00377                                         return $this->parseResponse($http->incoming_headers, $this->responseData);
00378                                 }
00379                         break;
00380                         default:
00381                                 $this->setError('no transport found, or selected transport is not yet supported!');
00382                         return false;
00383                         break;
00384                 }
00385         }
00386 
00395     function parseResponse($headers, $data) {
00396                 $this->debug('Entering parseResponse() for data of length ' . strlen($data) . ' and type ' . $headers['content-type']);
00397                 if (!strstr($headers['content-type'], 'text/xml')) {
00398                         $this->setError('Response not of type text/xml');
00399                         return false;
00400                 }
00401                 if (strpos($headers['content-type'], '=')) {
00402                         $enc = str_replace('"', '', substr(strstr($headers["content-type"], '='), 1));
00403                         $this->debug('Got response encoding: ' . $enc);
00404                         if(eregi('^(ISO-8859-1|US-ASCII|UTF-8)$',$enc)){
00405                                 $this->xml_encoding = strtoupper($enc);
00406                         } else {
00407                                 $this->xml_encoding = 'US-ASCII';
00408                         }
00409                 } else {
00410                         // should be US-ASCII, but for XML, let's be pragmatic and admit UTF-8 is most common
00411                         $this->xml_encoding = 'UTF-8';
00412                 }
00413                 $this->debug('Use encoding: ' . $this->xml_encoding . ' when creating soap_parser');
00414                 $parser = new soap_parser($data,$this->xml_encoding,$this->operation,$this->decode_utf8);
00415                 // add parser debug data to our debug
00416                 $this->debug($parser->debug_str);
00417                 // if parse errors
00418                 if($errstr = $parser->getError()){
00419                         $this->setError( $errstr);
00420                         // destroy the parser object
00421                         unset($parser);
00422                         return false;
00423                 } else {
00424                         // get SOAP headers
00425                         $this->responseHeaders = $parser->getHeaders();
00426                         // get decoded message
00427                         $return = $parser->get_response();
00428             // add document for doclit support
00429             $this->document = $parser->document;
00430                         // destroy the parser object
00431                         unset($parser);
00432                         // return decode message
00433                         return $return;
00434                 }
00435          }
00436 
00443         function setHeaders($headers){
00444                 $this->requestHeaders = $headers;
00445         }
00446 
00453         function getHeaders(){
00454             if($this->responseHeaders != '') {
00455                         return $this->responseHeaders;
00456             }
00457         }
00458 
00468         function setHTTPProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '') {
00469                 $this->proxyhost = $proxyhost;
00470                 $this->proxyport = $proxyport;
00471                 $this->proxyusername = $proxyusername;
00472                 $this->proxypassword = $proxypassword;
00473         }
00474 
00483         function setCredentials($username, $password, $authtype = 'basic') {
00484                 $this->username = $username;
00485                 $this->password = $password;
00486                 $this->authtype = $authtype;
00487         }
00488         
00495         function setHTTPEncoding($enc='gzip, deflate'){
00496                 $this->http_encoding = $enc;
00497         }
00498         
00504         function useHTTPPersistentConnection(){
00505                 $this->persistentConnection = true;
00506         }
00507         
00518         function getDefaultRpcParams() {
00519                 return $this->defaultRpcParams;
00520         }
00521 
00530         function setDefaultRpcParams($rpcParams) {
00531                 $this->defaultRpcParams = $rpcParams;
00532         }
00533         
00540         function getProxy(){
00541                 $evalStr = '';
00542                 foreach($this->operations as $operation => $opData){
00543                         if($operation != ''){
00544                                 // create param string
00545                                 $paramStr = '';
00546                                 if(sizeof($opData['input']['parts']) > 0){
00547                                         foreach($opData['input']['parts'] as $name => $type){
00548                                                 $paramStr .= "\$$name,";
00549                                         }
00550                                         $paramStr = substr($paramStr,0,strlen($paramStr)-1);
00551                                 }
00552                                 $opData['namespace'] = !isset($opData['namespace']) ? 'http://testuri.com' : $opData['namespace'];
00553                                 $evalStr .= "function $operation ($paramStr){
00554                                         // load params into array
00555                                         \$params = array($paramStr);
00556                                         return \$this->call('$operation',\$params,'".$opData['namespace']."','".(isset($opData['soapAction']) ? $opData['soapAction'] : '')."');
00557                                 }";
00558                                 unset($paramStr);
00559                         }
00560                 }
00561                 $r = rand();
00562                 $evalStr = 'class soap_proxy_'.$r.' extends soap_client {
00563                                 '.$evalStr.'
00564                         }';
00565                 //print "proxy class:<pre>$evalStr</pre>";
00566                 // eval the class
00567                 eval($evalStr);
00568                 // instantiate proxy object
00569                 eval("\$proxy = new soap_proxy_$r('');");
00570                 // transfer current wsdl data to the proxy thereby avoiding parsing the wsdl twice
00571                 $proxy->endpointType = 'wsdl';
00572                 $proxy->wsdlFile = $this->wsdlFile;
00573                 $proxy->wsdl = $this->wsdl;
00574                 $proxy->operations = $this->operations;
00575                 $proxy->defaultRpcParams = $this->defaultRpcParams;
00576                 // transfer other state
00577                 $proxy->username = $this->username;
00578                 $proxy->password = $this->password;
00579                 $proxy->proxyhost = $this->proxyhost;
00580                 $proxy->proxyport = $this->proxyport;
00581                 $proxy->proxyusername = $this->proxyusername;
00582                 $proxy->proxypassword = $this->proxypassword;
00583                 $proxy->timeout = $this->timeout;
00584                 $proxy->response_timeout = $this->response_timeout;
00585                 $proxy->http_encoding = $this->http_encoding;
00586                 $proxy->persistentConnection = $this->persistentConnection;
00587                 return $proxy;
00588         }
00589 
00597         function getHTTPBody($soapmsg) {
00598                 return $soapmsg;
00599         }
00600         
00609         function getHTTPContentType() {
00610                 return 'text/xml';
00611         }
00612         
00622         function getHTTPContentTypeCharset() {
00623                 return $this->soap_defencoding;
00624         }
00625 
00626         /*
00627         * whether or not parser should decode utf8 element content
00628     *
00629     * @return   always returns true
00630     * @access   public
00631     */
00632     function decodeUTF8($bool){
00633                 $this->decode_utf8 = $bool;
00634                 return true;
00635     }
00636 }
00637 ?>

Generated on Fri Dec 13 2013 09:06:40 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1