使用 PHP SOAP 來建立一個簡單的 Web Service。
阿新 • • 發佈:2018-12-11
訪問:
http://www.debug.com/php-soap-demo.php?client=22
結果:
apache:
<VirtualHost _default_:80> DocumentRoot "E:\www\test\debug" ServerName www.debug.com ServerAlias debug.com <Directory "E:\www\test\debug"> Options -Indexes +FollowSymLinks +ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory> </VirtualHost>
code:: php-soap-demo.php
<?php /*-------------*/ if(isset($_GET['client'])){//fixme client index - 客戶端入口 try{ // non-wsdl方式呼叫web service // 建立 SoapClient 物件 $soap = new SoapClient(null,array('location'=>"http://www.debug.com/php-soap-demo.php",'uri'=>'php-soap-demo.php')); // 呼叫函式 $result1 = $soap->getName(); $result2 = $soap->__soapCall("getHost",array()); echo $result1."<br/>"; echo $result2; } catch(SoapFault $e){ echo $e->getMessage(); }catch(Exception $e){ echo $e->getMessage(); } } /*-------------*/ //fixme server index //request Class Class Request { //base config protected $config = [ 'app'=> '徐鍋部落格!', 'host'=>'localhost:3038' ]; //construct public function __construct($config= []) { $this->config = array_merge($this->config,$config); } //get attr config public function __get($name){ return $this->config[$name]; } //soap method public function getName() { return $this->app; } //soap method public function getHost() { return $this->host; } } // Create SoapServer OBJECT $server = new SoapServer(null,array("location"=>"http://www.debug.com/php-soap-demo.php","uri"=>"php-soap-demo.php")); // EXPORT Request 類中的全部函式 $server->setClass("Request"); // 處理一個SOAP請求,呼叫必要的功能,併發送回一個響應。 $server->handle();