[2] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | |
---|
| 4 | SOAPx4 |
---|
| 5 | by Dietrich Ayala (C) 2001 dietrich@ganx4.com |
---|
| 6 | |
---|
| 7 | This project began based on code from the 2 projects below, |
---|
| 8 | and still contains some original code. The licenses of both must be respected. |
---|
| 9 | |
---|
| 10 | XML-RPC for PHP |
---|
| 11 | originally by Edd Dumbill (C) 1999-2000 |
---|
| 12 | |
---|
| 13 | SOAP for PHP |
---|
| 14 | by Victor Zou (C) 2000-2001 <victor@gigaideas.com.cn> |
---|
| 15 | |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* changelog: |
---|
| 19 | 2001-07-04 |
---|
| 20 | - abstract type system to support either 1999 or 2001 schema (arg, typing still needs much |
---|
| 21 | solidification.) |
---|
| 22 | - implemented proxy support, based on sample code from miles lott <milos@speakeasy.net> |
---|
| 23 | - much general cleanup of code & cleaned out what was left of original xml-rpc/gigaideas code |
---|
| 24 | - implemented a transport argument into send() that allows you to specify different transports |
---|
| 25 | (assuming you have implemented the function, and added it to the conditional statement in send() |
---|
| 26 | - abstracted the determination of charset in Content-type header |
---|
| 27 | 2001-07-5 |
---|
| 28 | - fixed more weird type/namespace issues |
---|
| 29 | */ |
---|
| 30 | |
---|
| 31 | // $path can be a complete endpoint url, with the other parameters left blank: |
---|
| 32 | // $soap_client = new soap_client("http://path/to/soap/server"); |
---|
| 33 | |
---|
| 34 | /* soapx4 high level class |
---|
| 35 | |
---|
| 36 | usage: |
---|
| 37 | |
---|
| 38 | // instantiate client with server info |
---|
| 39 | $soapclient = new soapclient( string path [ ,boolean wsdl] ); |
---|
| 40 | |
---|
| 41 | // call method, get results |
---|
| 42 | echo $soapclient->call( string methodname [ ,array parameters] ); |
---|
| 43 | |
---|
| 44 | // bye bye client |
---|
| 45 | unset($soapclient); |
---|
| 46 | |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | class soapclient |
---|
| 50 | { |
---|
| 51 | function soapclient($endpoint,$wsdl=False,$portName=False) |
---|
| 52 | { |
---|
| 53 | $this->debug_flag = True; |
---|
| 54 | $this->endpoint = $endpoint; |
---|
| 55 | $this->portName = False; |
---|
| 56 | |
---|
| 57 | // make values |
---|
| 58 | if($wsdl) |
---|
| 59 | { |
---|
| 60 | $this->endpointType = 'wsdl'; |
---|
| 61 | if($portName) |
---|
| 62 | { |
---|
| 63 | $this->portName = $portName; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | function call($method,$params='',$namespace=false,$soapAction=false) |
---|
| 69 | { |
---|
| 70 | if($this->endpointType == 'wsdl') |
---|
| 71 | { |
---|
| 72 | // instantiate wsdl class |
---|
| 73 | $this->wsdl = CreateObject('phpgwapi.wsdl',$this->endpoint); |
---|
| 74 | // get portName |
---|
| 75 | if(!$this->portName) |
---|
| 76 | { |
---|
| 77 | $this->portName = $this->wsdl->getPortName($method); |
---|
| 78 | } |
---|
| 79 | // get endpoint |
---|
| 80 | if(!$this->endpoint = $this->wsdl->getEndpoint($this->portName)) |
---|
| 81 | { |
---|
| 82 | die("no port of name '$this->portName' in the wsdl at that location!"); |
---|
| 83 | } |
---|
| 84 | $this->debug("endpoint: $this->endpoint"); |
---|
| 85 | $this->debug("portName: $this->portName"); |
---|
| 86 | } |
---|
| 87 | // get soapAction |
---|
| 88 | if(!$soapAction) |
---|
| 89 | { |
---|
| 90 | if($this->endpointType != 'wsdl') |
---|
| 91 | { |
---|
| 92 | die("method call requires soapAction if wsdl is not available!"); |
---|
| 93 | } |
---|
| 94 | if(!$soapAction = $this->wsdl->getSoapAction($this->portName,$method)) |
---|
| 95 | { |
---|
| 96 | die("no soapAction for operation: $method!"); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | $this->debug("soapAction: $soapAction"); |
---|
| 100 | // get namespace |
---|
| 101 | if(!$namespace) |
---|
| 102 | { |
---|
| 103 | if($this->endpointType != 'wsdl') |
---|
| 104 | { |
---|
| 105 | die("method call requires namespace if wsdl is not available!"); |
---|
| 106 | } |
---|
| 107 | if(!$namespace = $this->wsdl->getNamespace($this->portName,$method)) |
---|
| 108 | { |
---|
| 109 | die("no soapAction for operation: $method!"); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | $this->debug("namespace: $namespace"); |
---|
| 113 | |
---|
| 114 | // make message |
---|
| 115 | $soapmsg = CreateObject('phpgwapi.soapmsg',$method,$params,$namespace); |
---|
| 116 | /* _debug_array($soapmsg); */ |
---|
| 117 | |
---|
| 118 | // instantiate client |
---|
| 119 | $dbg = "calling server at '$this->endpoint'..."; |
---|
| 120 | if($soap_client = CreateObject('phpgwapi.soap_client',$this->endpoint)) |
---|
| 121 | { |
---|
| 122 | //$soap_client->debug_flag = true; |
---|
| 123 | $this->debug($dbg.'instantiated client successfully'); |
---|
| 124 | $this->debug("client data:<br>server: $soap_client->server<br>path: $soap_client->path<br>port: $soap_client->port"); |
---|
| 125 | // send |
---|
| 126 | $dbg = "sending msg w/ soapaction '$soapAction'..."; |
---|
| 127 | if($return = $soap_client->send($soapmsg,$soapAction)) |
---|
| 128 | { |
---|
| 129 | $this->request = $soap_client->outgoing_payload; |
---|
| 130 | $this->response = $soap_client->incoming_payload; |
---|
| 131 | $this->debug($dbg . "sent message successfully and got a '$return' back"); |
---|
| 132 | // check for valid response |
---|
| 133 | if(get_class($return) == 'soapval') |
---|
| 134 | { |
---|
| 135 | // fault? |
---|
| 136 | if(eregi('fault',$return->name)) |
---|
| 137 | { |
---|
| 138 | $this->debug('got fault'); |
---|
| 139 | $faultArray = $return->decode(); |
---|
| 140 | @reset($faultArray); |
---|
| 141 | while(list($k,$v) = @each($faultArray)) |
---|
| 142 | /* foreach($faultArray as $k => $v) */ |
---|
| 143 | { |
---|
| 144 | print "$k = $v<br>"; |
---|
| 145 | } |
---|
| 146 | return false; |
---|
| 147 | } |
---|
| 148 | else |
---|
| 149 | { |
---|
| 150 | $returnArray = $return->decode(); |
---|
| 151 | if(is_array($returnArray)) |
---|
| 152 | { |
---|
| 153 | return array_shift($returnArray); |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | { |
---|
| 157 | $this->debug("didn't get array back from decode() for $return->name"); |
---|
| 158 | return false; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | $this->debug("didn't get soapval object back from client"); |
---|
| 165 | return false; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | $this->debug('client send/recieve error'); |
---|
| 171 | return false; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | function debug($string) |
---|
| 177 | { |
---|
| 178 | if($this->debug_flag) |
---|
| 179 | { |
---|
| 180 | print $string . '<br>'; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | ?> |
---|