SOAP webservice接口

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

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

<?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实现身份认证

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

 客户端

<?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,这儿我理解得不是太透彻,不好说。

$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;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值