MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
class.phpwsdlclient.php
Ir para a documentação deste ficheiro.
1<?php
2
3/*
4PhpWsdl - Generate WSDL from PHP
5Copyright (C) 2011 Andreas Zimmermann, wan24.de
6
7This program is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program; if not, see <http://www.gnu.org/licenses/>.
18*/
19
20if(basename($_SERVER['SCRIPT_FILENAME'])==basename(__FILE__))
21 exit;
22
23// To use PhpWsdlClient, you need to load it seperatly from PhpWsdl. Or you
24// just load this class and let it autoload PhpWsdl
25
26require_once(dirname(__FILE__).'/class.phpwsdl.php');
27
40 public static $VERSION='2.2';
49 public static $methodRx='/^([^\s]+)\s+([^\‍(]+)\‍(([^\‍)]+)?\‍).*$/';
57 public static $typeRx='/^([^\s]+)\s([^\[|\{|\s]+)[\[|\{|\s].*$/s';
65 public static $elementRx='/^\s*([^\s]+)\s+([^\s|;]+);.*$/';
71 public $WsdlUri;
77 public $Client=null;
83 public $Options;
101 public $ServiceName=null;
107 public $EndPoint=null;
113 public $NameSpace=null;
119 public $Server=null;
125 public $WSDL=null;
131 public $Warnings=Array();
139 public $HttpUser=null;
145 public $HttpPassword=null;
151 public $UseSoapHttpAuth=true;
157 public $Debugging=false;
158
166 public function PhpWsdlClient($wsdlUri,$options=null,$requestHeaders=null,$clientOptions=Array()){
167 self::__construct($wsdlUri, $options, $requestHeaders, $clientOptions);
168 }
169
170 public function __construct($wsdlUri,$options=null,$requestHeaders=null,$clientOptions=Array()){
171 PhpWsdl::Debug('New PhpWsdlClient '.$wsdlUri);
172 $this->WsdlUri=$wsdlUri;
173 $this->Options=$options;
174 $this->RequestHeaders=$requestHeaders;
175 $this->ClientOptions=array_merge(
176 Array(
177 'soap_version' => SOAP_1_1|SOAP_1_2,
178 'encoding' => 'UTF-8',
179 'compression' => SOAP_COMPRESSION_ACCEPT|SOAP_COMPRESSION_GZIP|9,
180 'connection_timeout'=>5
181 ),
182 $clientOptions
183 );
184 $this->GetWsdlFromCache();
185 }
186
194 public function FetchWsdl($wsdlUri=null,$options=Array()){
195 if(is_null($wsdlUri))
196 $wsdlUri=$this->WsdlUri;
197 PhpWsdl::Debug('Fetch WSDL from '.$wsdlUri);
198 if(!is_file($wsdlUri)&&function_exists('curl_init')&&!is_null($this->HttpUser)){
199 // Fetch with http Auth (credits to faebu :)
200 PhpWsdl::Debug('Try CURL for http Auth');
201 $ch=curl_init();
202 $credit=($this->HttpUser.':'.$this->HttpPassword);
203 curl_setopt_array($ch,array_merge(
204 Array(
205 CURLOPT_URL => $wsdlUri,
206 CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
207 CURLOPT_USERPWD => $credit,
208 CURLOPT_TIMEOUT => 10,
209 CURLOPT_RETURNTRANSFER => 1
210 ),
211 $options
212 ));
213 $wsdl=curl_exec($ch);
214 if($wsdl===false){
215 PhpWsdl::Debug('Could not fetch WSDL with CURL: '.curl_error($ch));
216 @curl_close($ch);
217 }else{
218 curl_close($ch);
219 return $wsdl;
220 }
221 }
222 // Fetch without authentification
223 return file_get_contents($wsdlUri);
224 }
225
235 public function DoRequest($method,$param,$options=Array(),$requestHeaders=Array()){
236 PhpWsdl::Debug('Sending request '.$method);
237 $options=array_merge($this->Options,$options);
238 $requestHeaders=array_merge($this->RequestHeaders,$requestHeaders);
239 $client=$this->GetClient();
240 $res=$client->__soapCall($method,$param,$options,$requestHeaders);
241 if(is_soap_fault($res))
242 PhpWsdl::Debug('SOAP error #'.$res->faultcode.': '.$res->faultstring);
243 if($this->Debugging){
244 PhpWsdl::Debug('Parameters: '.print_r($param,true));
245 PhpWsdl::Debug('Options: '.print_r($options,true));
246 PhpWsdl::Debug('Headers: '.print_r($requestHeaders,true));
247 PhpWsdl::Debug('Result: '.print_r($res,true));
248 PhpWsdl::Debug('Request: '.$client->__getLastRequest());
249 PhpWsdl::Debug('Request headers: '.$client->__getLastRequestHeaders());
250 PhpWsdl::Debug('Response: '.$client->__getLastResponse());
251 PhpWsdl::Debug('Response headers: '.$client->__getLastResponseHeaders());
252 }
253 return $res;
254 }
255
263 public function GetClient($uri=null,$options=Array()){
264 if(is_null($this->Client)){
265 if(is_null($uri))
266 $uri=$this->WsdlUri;
267 PhpWsdl::Debug('Create a PHP SoapClient object in PhpWsdlClient from '.$uri);
268 if(!is_null(PhpWsdl::$CacheFolder)){
269 $wsdlFile=$this->GetCacheFileName($uri);
270 if(!is_null($this->GetWsdlFromCache($wsdlFile))){
271 PhpWsdl::Debug('Use cached WSDL from '.$wsdlFile);
272 $uri=$wsdlFile;
273 }else{
274 $wsdl=$this->FetchWsdl($uri);
275 if($wsdl!==false)
276 if($this->WriteWsdlToCache($wsdl,$uri,$wsdlFile,true)){
277 PhpWsdl::Debug('Using cached WSDL from '.$wsdlFile);
278 $uri=$wsdlFile;
279 }
280 }
281 }
282 if(
283 !is_null($this->HttpUser)&&
284 !isset($this->ClientOptions['login'])&&
285 !isset($options['login'])&&
286 $this->UseSoapHttpAuth
287 ){
288 PhpWsdl::Debug('Using http Auth options');
289 $options['login']=$this->HttpUser;
290 $options['password']=$this->HttpPassword;
291 }
292 if($this->Debugging){
293 PhpWsdl::Debug('Debugging enabled');
294 $options['trace']=true;
295 }
296 $this->Client=new SoapClient($uri,array_merge(
297 $this->ClientOptions,
298 $options
299 ));
300 }
301 return $this->Client;
302 }
303
310 public function CreateServerFromWsdl($soap=null){
311 PhpWsdl::Debug('Create/Fill a PhpWsdl object from WSDL');
312 if(is_null($soap)){
313 if(!is_null($this->Server)){
314 PhpWsdl::Debug('Return existing object');
315 return $this->Server;
316 }
318 }else{
319 PhpWsdl::Debug('Use existing object');
320 }
321 if(!is_null($soap->GetWsdlFromCache())){
322 PhpWsdl::Debug('Server created from cached values');
323 if(is_null($this->Server))
324 $this->Server=$soap;
325 return $soap;
326 }
327 // Configuration
328 $soap->WsdlUri=$this->WsdlUri;
329 $this->ParseWsdl();
330 if(!is_null($this->ServiceName))
332 if(!is_null($this->NameSpace))
333 $soap->NameSpace=$this->NameSpace;
334 $client=$this->GetClient();
335 // Methods
336 $fnc=$client->__getFunctions();
337 $i=-1;
338 $len=sizeof($fnc);
339 while(++$i<$len){
340 $f=$fnc[$i];
341 list(
342 $type,
343 $method,
344 $temp
345 )=explode("\t",preg_replace(self::$methodRx,"$1\t$2\t$3",$f));
346 PhpWsdl::Debug('Found method #'.$i.' '.$method);
347 if(!is_null($soap->GetMethod($method))){
348 $this->Warn('WARNING: Double method detected!');
349 continue;
350 }
351 $m=new PhpWsdlMethod($method);
352 $temp=explode(' ',$temp);
353 $pLen=sizeof($temp);
354 for($j=0;$j<$pLen-1;$j++){
355 list(
356 $t,
357 $n
358 )=Array(
359 $temp[$j],
360 $temp[$j+1]
361 );
362 PhpWsdl::Debug('Found parameter #'.$j.' '.$n.' type of '.$t);
363 $m->Param[]=new PhpWsdlParam(substr($n,1),$t);
364 }
365 if($type!='void')
366 $m->Return=new PhpWsdlParam('return',$type);
367 $soap->Methods[]=$m;
368 }
369 // Types
370 $typ=$client->__getTypes();
371 $i=-1;
372 $len=sizeof($typ);
373 while(++$i<$len){
374 $t=$typ[$i];
375 list(
376 $type,
377 $name
378 )=explode("\t",preg_replace(self::$typeRx,"$1\t$2",$t));
379 PhpWsdl::Debug('Found type #'.$i.' '.$name.' type of '.$type);
380 if(!is_null($soap->GetType($name))){
381 $this->Warn('WARNING: Double type detected!');
382 continue;
383 }
384 $arr=strpos($t,'[]')>-1;
385 if($arr){
386 PhpWsdl::Debug('Array type');
387 $y=new PhpWsdlComplex($name);
388 $y->Type=$type;
389 $y->IsArray=true;
390 $soap->Types[]=$y;
391 }else if($type=='struct'){
392 PhpWsdl::Debug('Complex type');
393 $el=Array();
394 $temp=explode("\n",$t);
395 $j=0;
396 $eLen=sizeof($temp)-1;
397 while(++$j<$eLen){
398 list(
399 $p,
400 $n
401 )=explode("\t",preg_replace(self::$elementRx,"$1\t$2",$temp[$j]),2);
402 PhpWsdl::Debug('Found element #'.$j.' '.$n.' type of '.$p);
403 $el[]=new PhpWsdlElement($n,$p);
404 }
405 $y=new PhpWsdlComplex($name,$el);
406 $y->IsArray=false;
407 $soap->Types[]=$y;
408 }else{
409 $this->Warn('WARNING: Could not create type '.$t);
410 }
411 }
412 if(is_null($this->Server))
413 $this->Server=$soap;
414 return $soap;
415 }
416
420 public function ParseWsdl(){
421 if(!is_null($this->ServiceName)&&!is_null($this->EndPoint)&&!is_null($this->NameSpace))
422 return;
423 PhpWsdl::Debug('Parse WSDL');
424 $wsdl=$this->WSDL;
425 $writeCache=false;
426 if(is_null($wsdl)){
427 $wsdl=$this->FetchWsdl();
428 if($wsdl===false)
429 throw(new Exception('Could not fetch WSDL'));
430 $writeCache=true;
431 }
432 $xml=new DOMDocument();
433 if(!$xml->loadXML($wsdl))
434 throw(new Exception('Invalid WSDL'));
435 $x=new DOMXPath($xml);
436 // Namespace
437 $temp=$x->query("/*[local-name()='definitions']/@targetNamespace");
438 if($temp->length>0){
439 $temp=$temp->item(0);
440 PhpWsdl::Debug('Namespace '.$temp->value);
441 $this->NameSpace=$temp->value;
442 }else{
443 $this->Warn('WARNING: No namespace found');
444 }
445 // Webservice name
446 $temp=$x->query("/*[local-name()='definitions']/*[local-name()='service']/@name");
447 if($temp->length>0){
448 $temp=$temp->item(0);
449 PhpWsdl::Debug('Name '.$temp->value);
450 $this->ServiceName=$temp->value;
451 }else{
452 $this->Warn('WARNING: No name found');
453 }
454 // Endpoint URI
455 $temp=$x->query("/*[local-name()='definitions']/*[local-name()='service']/*[local-name()='port']/*[local-name()='address']/@location");
456 if($temp->length>0){
457 $temp=$temp->item(0);
458 PhpWsdl::Debug('Endpoint '.$temp->value);
459 $this->EndPoint=$temp->value;
460 }else{
461 $this->Warn('WARNING: No endpoint found');
462 }
463 // Caching
464 $this->WriteWsdlToCache($wsdl,null,null,$writeCache);
465 }
466
474 public function CreatePhpSoapClient($filename=null,$options=Array()){
475 $php=$this->CreateServerFromWsdl()->OutputPhp(false,false,$options);
476 if(!is_null($filename))
477 if(file_put_contents($filename,$php)===false)
478 PhpWsdl::Debug('Could not write file '.$filename);
479 return $php;
480 }
481
487 private function Warn($str){
488 $this->Warnings[]=$str;
489 PhpWsdl::Debug($str);
490 }
491
498 public function GetCacheFileName($wsdluri=null){
499 $data=Array(
500 'client' => $this,
501 'wsdluri' => $wsdluri,
502 'filename' => (is_null(PhpWsdl::$CacheFolder))?null:PhpWsdl::$CacheFolder.'/client-'.sha1((is_null($wsdluri))?$this->WsdlUri:$wsdluri).'.wsdl'
503 );
505 'ClientCacheFileNameHook',
506 $data
507 );
508 return $data['filename'];
509 }
510
517 public function CacheFileExists($file=null){
518 if(is_null($file))
519 $file=$this->GetCacheFileName();
520 PhpWsdl::Debug('Check cache file exists '.$file);
521 return file_exists($file)&&file_exists($file.'.cache');
522 }
523
530 public function IsCacheValid($file=null){
531 PhpWsdl::Debug('Check cache valid');
532 if(is_null($file))
533 $file=$this->GetCacheFileName();
534 if(!$this->CacheFileExists($file))
535 return false;
536 return PhpWsdl::$CacheTime<0||time()-file_get_contents($file.'.cache')<=PhpWsdl::$CacheTime;
537 }
538
547 public function GetWsdlFromCache($file=null,$force=false,$nounserialize=false){
548 PhpWsdl::Debug('Get WSDL from cache');
549 if(!is_null($this->WSDL))
550 return $this->WSDL;
551 if(is_null($file))
552 $file=$this->GetCacheFileName();
553 if(!$force){
554 if(!$this->IsCacheValid($file))
555 return null;
556 }else if(!$this->CacheFileExists($file)){
557 return null;
558 }
559 $this->WSDL=file_get_contents($file);
560 if(!$nounserialize){
561 PhpWsdl::Debug('Unserialize data');
562 $data=unserialize(file_get_contents($file.'.obj'));
563 $this->ServiceName=$data['servicename'];
564 $this->EndPoint=$data['endpoint'];
565 $this->NameSpace=$data['namespace'];
567 'ClientReadCacheHook',
568 Array(
569 'client' => $this,
570 'data' => &$data
571 )
572 );
573 if($data['version']!=self::$VERSION){
574 PhpWsdl::Debug('Could not use cache from version '.$data['version']);
575 $this->ServiceName=null;
576 $this->EndPoint=null;
577 $this->NameSpace=null;
578 $this->WSDL=null;
579 return null;
580 }
581 }
582 return $this->WSDL;
583 }
584
594 public function WriteWsdlToCache($wsdl=null,$wsdluri=null,$file=null,$force=false){
595 PhpWsdl::Debug('Write WSDL to the cache');
596 if(is_null($wsdluri))
597 $wsdluri=$this->WsdlUri;
598 if($wsdluri==$this->WsdlUri&&!is_null($wsdl))
599 $this->WSDL=$wsdl;
600 if(is_null($wsdl)){
601 if(is_null($this->WSDL)){
602 PhpWsdl::Debug('No WSDL');
603 return false;// WSDL not defined
604 }
605 $wsdl=$this->WSDL;
606 }
607 if(is_null($file)){
608 $file=$this->GetCacheFileName($wsdluri);
609 if(is_null($file)){
610 PhpWsdl::Debug('No cache file');
611 return false;// No cache file
612 }
613 }
614 $temp=substr($file,0,1);
615 if($temp!='/'&&$temp!='.'){
616 if(is_null(PhpWsdl::$CacheFolder)){
617 PhpWsdl::Debug('No cache folder');
618 return false;// No cache folder
619 }
620 $file=PhpWsdl::$CacheFolder.'/'.$file;
621 }
622 if(!$force)
623 if($this->IsCacheValid($file)){
624 PhpWsdl::Debug('Cache is still valid');
625 return true;// Existing cache is still valid
626 }
627 PhpWsdl::Debug('Write to '.$file);
628 if(file_put_contents($file,$wsdl)===false){
629 PhpWsdl::Debug('Could not write to cache');
630 return false;// Error writing to cache
631 }
632 if(file_put_contents($file.'.cache',time())===false){
633 PhpWsdl::Debug('Could not write cache time file');
634 return false;// Error writing to cache
635 }
636 $data=Array(
637 'version' => self::$VERSION,
638 'servicename' => $this->ServiceName,
639 'endpoint' => $this->EndPoint,
640 'namespace' => $this->NameSpace
641 );
643 'ClientWriteCacheHook',
644 Array(
645 'client' => $this,
646 'data' => &$data
647 )
648 );
649 if(file_put_contents($file.'.obj',serialize($data))===false){
650 PhpWsdl::Debug('Could not write serialized cache');
651 return false;
652 }
653 return true;
654 }
655
663 public function __call($method,$param){
664 return $this->DoRequest($method,$param);
665 }
666}
DoRequest($method, $param, $options=Array(), $requestHeaders=Array())
CacheFileExists($file=null)
GetClient($uri=null, $options=Array())
CreatePhpSoapClient($filename=null, $options=Array())
CreateServerFromWsdl($soap=null)
WriteWsdlToCache($wsdl=null, $wsdluri=null, $file=null, $force=false)
PhpWsdlClient($wsdlUri, $options=null, $requestHeaders=null, $clientOptions=Array())
FetchWsdl($wsdlUri=null, $options=Array())
__call($method, $param)
IsCacheValid($file=null)
GetWsdlFromCache($file=null, $force=false, $nounserialize=false)
__construct($wsdlUri, $options=null, $requestHeaders=null, $clientOptions=Array())
GetCacheFileName($wsdluri=null)
static $CacheFolder
static Debug($str)
static $CacheTime
static CreateInstance( $nameSpace=null, $endPoint=null, $cacheFolder=null, $file=null, $name=null, $methods=null, $types=null, $outputOnRequest=false, $runServer=false)
static CallHook($name, $data=null)
$soap
Definição demo.php:14