您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

SOAP webservice接口

2024/4/13 23:52:11发布21次查看
php 中,在 php.ini 文件中开启了 php_soap.dll 扩展后,就可以支持 soap 了。
在soap扩展库中,主要包括三种对象。
1、soapserver
    用于创建php服务器端页面时定义可被调用的函数及返回响应数据。创建一个soapserver对象的语法格式如下:
    $soap = new soapserver($wsdl, $array);
    其中,$wsdl为shoap使用得wsdl文件,wsdl 是描述 web service的一种标准格式,若将$wsdl设置为null,则表示不使用wsdl模式。$array是soapserver的属性信息,是一个数组。
    soapserver对象的addfunction方法是用来声明哪个函数可以被客户端调用,语法格式如下:
    $soap->addfunction($function_name);
    其中,$soap是一个soapserver对象,$function_name是需要被调用的函数名。
    soapserver对象的handle方法用来处理用户输入并调用相应的函数,最后返回给客户端处理的结果。语法格式如下:
    $soap->handle([$soap_request]);
    其中,$soap是一个soapserver对象,$soap_request是一个可选参数,用来表示用户的请求信息。如果不指定$soap_request,则表示服务器将接收用户的全部请求。
2、soapcliet
    用于调用远程服务器上的soapserver页面,并实现了对相应函数的调用。创建一个soapclient对象的语法格式如下:
    $soap = new soapclient($wsdl,$array);
    其中,参数$wsdl和$array与soapserver相同。
    创建soapclient对象后,调用服务端页面中的函数相当于调用了soapclient的方法,创建语法如下:
    $soap->user_function($params);
    其中,$soap是一个soapclient对象,user_function是服务器端要调用的函数,$params 是要传入函数的参数。
3、soapfault
    soapfault用于生成soap访问过程中可能出现的错误。创建一个soapfault对象的语法格式如下:
    $fault = new soapfault($faultcode,$faultstring);
    其中,$faultcode是用户定义的错误代码,$faultstring是用户自定义的错误信息。soapfault 对象会在服务器端页面出现错误时自动生成,或者通过用户自行创建soapfault对象时生成。对于 soap访问时出现的错误,客户端可通过捕捉soapfalut对象来获得相应的错误信息。
    在客户端捕获soapfault对象后,可以通过下面的代码获得错误代码和错误信息:
    $fault->faultcode;//错误代码
    $fault->faultstring;//错误信息
    其中,$fault是在前面创建的soapfault对象。
不论是soapserver还是soapclient,都接收两个参数,其中第二个参数是option,它支持若干选项,这里我们用到的有:
uri:命名空间,客户端和服务端需要使用相同的命名空间
location:客户端用,用来指定服务端程序的访问地址,也就是本例第二段代码的程序地址。
trace:客户端用,为true时可以获取服务端和客户端通信的内容,以供调试。
soapserver.php
java代码  
<?php
//先创建一个soapserver对象实例,然后将我们要暴露的函数注册,
//最后的handle()用来处理接受的soap请求
error_reporting(7); //正式发布时,设为 0
date_default_timezone_set('prc'); //设置时区
/* 几个供client端调用的函数 */
function reverse($str)
{
$retval = '';
if (strlen($str) < 1) {
return new soapfault ('client', '', 'invalid string');
}
for ($i = 1; $i <= strlen($str); $i++) {
$retval .= $str [(strlen($str) - $i)];
}
return $retval;
}
function add2numbers($num1, $num2)
{
if (trim($num1) != intval($num1)) {
return new soapfault ('client', '', 'the first number is invalid');
}
if (trim($num2) != intval($num2)) {
return new soapfault ('client', '', 'the second number is invalid');
}
return ($num1 + $num2);
}
function gettime()
{
$time = date('y-m-d h:i:s', time());
return $time;
}
$soap = new soapserver (null, array('uri' => httr://test-rui));  
$soap->addfunction('reverse');  
$soap->addfunction('add2numbers');  
$soap->addfunction('gettime');  
$soap->addfunction(soap_functions_all);  
$soap->handle();  
?>  
 soapclient.php
java代码  
<?php
error_reporting(7);
try {
$client = new soapclient (null, array('location' => http://www.yiigo.com/soapserver.php, 'uri' => http://test-uri));
$str = this string will be reversed;  
    $reversed = $client->reverse($str);  
    echo if you reverse '$str', you will get '$reversed';
$n1 = 20;  
    $n2 = 33;  
    $sum = $client->add2numbers($n1, $n2);  
    echo <br>;  
    echo if you try $n1 + $n2, you will get $sum;
echo <br>;  
    echo the remoye system time is:  . $client->gettime();  
} catch (soapfault $fault) {  
    echo fault! code: . $fault->faultcode .  string: . $fault->faultstring;  
}  
?>  
if you reverse 'this string will be reversed', you will get 'desrever eb lliw gnirts siht'
if you try 20 + 33, you will get 53
the remoye system time is: 2012-05-28 16:14:29
通过soapheader实现身份认证
java代码  
<?php
class server
{
public function auth($a)
{
if ($a != '123456789') {
throw new soapfault('server', '用户身份认证信息错误');
}
}
public function say()
{
return 'hi';
}
}
$srv = new soapserver(null, array('uri' => 'http://localhost/namespace'));  
$srv->setclass('server');  
$srv->handle();   
 客户端
java代码  
<?php
$cli = new soapclient(null,
array('uri' => 'http://localhost/namespace/',  
        'location' => 'http://localhost/server.php',  
        'trace' => true));
//auth为服务端要处理的函数  12345689为参数    
$h = new soapheader('http://localhost/namespace/',  
    'auth', '123456789', false, soap_actor_next);  
$cli->__setsoapheaders(array($h));  
try {  
    echo $cli->say();  
} catch (exception $e) {  
    echo $e->getmessage();  
}   
注意观察server.php中的server类有一个方法“auth”,刚好与header的名称对应,方法auth的参数$u,就是soapheader的data,soapserver接收到这个请求会,先调用auth方法,并把“123456789”作为参数传递给该方法。mustunderstand参数为false时,即便没有auth这个方法,say方法也会被调用,但是如果它为true的话,如果auth方法不存在,就会返回一个soapfault告知该header没有被处理。actor参数指名那些role必须处理该header,这儿我理解得不是太透彻,不好说。
java代码  
$file = $this->getsoapwsdl();  
$client = new soapclient($file);//url可以通过浏览器访问,不能直接调用解决  
$param = array('userid' => 'test', 'merchantid' => 'test');  
$returnst = $client->checkuser($param);  
print_r($returnst->checkuserresult);
public function getsoapwsdl()  
{ //定期将url的文件保存到本地  
    $file = mage::getbasedir() . ds . 'data' . ds . 'shengda' . ds . 'export.wsdl';  
    if (time() > filemtime($file) + 7 * 86400) {  
        $url = http://jf.sdo.com/exchangescore/exchangeservice.asmx?wsdl;  
        include_once(bp . ds . lib/snoopy.class.php);  
        $snoopy = new snoopy;  
        $snoopy->fetch($url); //获取所有内容  
        $snoopy->read_timeout = 4;  
        $wsdl = $snoopy->results;  
        if ($snoopy->status == '200' && !$snoopy->timed_out) {  
            if (!is_dir(dirname($file))) {  
                mkdir(dirname($file));  
            }  
            file_put_contents($file, $wsdl);  
        }  
    }  
    return $file;  
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product