istanze di Web Service
Ogni applicazione può avere un componenti Web Service.
Categoria creazione e programmazione Web Service indipendente dalla lingua.
Questo capitolo introdurrà l'uso di estensione SOAP di PHP per creare un Web Service.
SOAP hanno due modalità di funzionamento, NO-WSDL e WSDL.
- NO-WSDL modalità: trasmettere informazioni da utilizzare parametri.
- Modalità WSDL: usando il nome file WSDL come parametro, ed estrarre le informazioni richieste dal WSDL del servizio.
Un esempio: PHP Web Service
Prima di iniziare un esempio, abbiamo bisogno di determinare se installare l'estensione PHP SOAP. Vedere phpinfo, il seguente messaggio appare già installato le estensioni SOAP:
In questo esempio, useremo il SOAP di PHP per creare un semplice Web Service.
codice file serverServer.php è il seguente:
<?php // SiteInfo 类用于处理请求 Class SiteInfo { /** * 返回网站名称 * @return string * */ public function getName(){ return "本教程"; } public function getUrl(){ return "www.w3big.com"; } } // 创建 SoapServer 对象 $s = new SoapServer(null,array("location"=>"http://localhost/soap/Server.php","uri"=>"Server.php")); // 导出 SiteInfo 类中的全部函数 $s->setClass("SiteInfo"); // 处理一个SOAP请求,调用必要的功能,并发送回一个响应。 $s->handle(); ?>
codice del fileClient.php client è la seguente:
<?php try{ // non-wsdl方式调用web service // 创建 SoapClient 对象 $soap = new SoapClient(null,array('location'=>"http://localhost/soap/Server.php",'uri'=>'Server.php')); // 调用函数 $result1 = $soap->getName(); $result2 = $soap->__soapCall("getUrl",array()); echo $result1."<br/>"; echo $result2; } catch(SoapFault $e){ echo $e->getMessage(); }catch(Exception $e){ echo $e->getMessage(); }
Poi visitiamo http: //localhost/soap/Client.php, risultati di output sono i seguenti: