PHP安裝SOAP擴展


PHP安裝SOAP擴展

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
------

  

PHP建立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就搭建好了,剩下的就是如何請求她了

PHP建立SOAP客戶端請求

*  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);

?>

  

------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM