1.安裝php-soap: yum install php-soap -y 2.在PHP的編譯參數中加入--enable-soap,如:
------ ./configure --prefix=/usr/local/php-5.2.12 \ --with-config-file-path=/usr/local/php-5.2.12/etc \ --enable-fastcgi --enable-force-cgi-redirect --with-curl \ --with-gd --with-ldap --with-snmp --enable-zip --enable-exif \ --with-pdo-mysql --with-mysql --with-mysqli \ --enable-soap ------
這裏咱們創建一個soap_function.php的文件,用於定義公開給請求的調用的函數
* file name:soap_function.php ------ <?php function get_str($str){ return 'hello '.$str; } function add_number($n1,$n2){ return $n1+$n2; } ?> ------
有了上步操做,咱們還要創建一個SOAP服務而且將定義好的函數註冊到服務中 * file name:soap_service.php ------
<?php include_once('soap_function.php');//導入函數文件 $soap = new SoapServer(null,array('uri'=>'http://zenw.org')); //創建SOAP服務實例 $soap->addFunction('get_str'); $soap->addFunction('sum_number'); //或者能夠 $soap->addFunction(array('get_str','sum_number')); $soap->addFunction(SOAP_FUNCTIONS_ALL); //常量SOAP_FUNCTIONS_ALL的含義在於將當前腳本所能訪問的全部function(包括一些系統function)都註冊到服務中 $soap->handle(); //SoapServer對象的handle方法用來處理用戶輸入並調用相應的函數,最後返回 ?>
------ 到這裏,一個SoapServer就搭建好了,剩下的就是如何請求她了
* file name:soap_client.php ------
<?php $client = new SoapClient(null,array('location'=>"http://192.168.3.229/soap/soap_service.php", //在SOAP服務器創建環節中創建的soap_service.php的URL網絡地址 'uri'=>'http://zenw.org')); $str = 'zenwong'; echo "get str is :".$client->get_str($str); echo "<br />"; echo 'sun number is:'.$client->sun_number(9,18); ?>
------