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

?>

  

------