php soap 使用实例

来源:互联网 发布:如何自学美工 编辑:程序博客网 时间:2024/05/22 13:25

SOAP 是基于XML和HTTP通讯协议,XML各个平台,各种语言都支持的一种语言。

WSDL 是网络服务描述语言(Web Services Description Language),是一种使用XML格式的文档。这种文档可描述某个Web Service。可规定服务的位置,及服务提供的操作。

不同语言之间需要通信(例如:php,java,c),可以通过SOAP,WSDL使不同操作系统,不同技术的编程语言互相通信。


php soap 扩展安装

扩展位置在php安装包的 ext/soap 目录,安装步骤:

cd php-5.3.15/ext/soapphpize./configuresudo makesudo make test
安装成功后在phpinfo可以看到soap扩展



SOAP有两种操作方式,NO-WSDL 与 WSDL

NO-WSDL模式:使用参数来传递要使用的信息

WSDL模式: 使用WSDL文件名作为参数,并从WSDL中提取服务所需的信息。(每次修改都需要修改client与server的wsdl文件,没有NO-WSDL模式灵活,以后再介绍这种模式的使用)


SOAP中主要用到三个类,SOAPServerSOAPClientSOAPFault


NO-WSDL模式:

soapHandle.class.php 处理请求

<?phpclass soapHandle{    public function strtolink($url=''){        return sprintf('<a href="%s">%s</a>', $url, $url);    }}?>
server.php soap服务端

<?php// 服务器验证if ($_SERVER['PHP_AUTH_USER']!='fdipzone' || $_SERVER['PHP_AUTH_PW']!='123456') {      header('WWW-Authenticate: Basic realm="MyFramework Realm"');      header('HTTP/1.0 401 Unauthorized');      echo "You must enter a valid login ID and password to access this resource.\n";      exit;}require("soapHandle.class.php"); // 处理请求的classtry{    $server = new SOAPServer(null, array('uri'=>'http://demo.fdipzone.com/soap/server.php'));    $server->setClass('soapHandle'); //设置处理的class    $server->handle();}catch(SOAPFault $f){    echo $f->faultString; // 打印出错信息}?>
client.php soap客户端

<?phptry{    $client = new SOAPClient(null, array(                        'location' => 'http://demo.fdipzone.com/soap/server.php', // 设置server路径                        'uri' => 'http://demo.fdipzone.com/soap/server.php',                        'login' => 'fdipzone', // HTTP auth login                        'password' => '123456' // HTTP auth password                    ));    echo $client->strtolink('http://blog.csdn.net/fdipzone').'<br>';               // 直接调用server方法    echo $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone')); // 间接调用server方法}catch(SOAPFault $e){    echo $e->getMessage();}?>

Header验证例子:

server.php

<?php// 服务器验证  if ($_SERVER['PHP_AUTH_USER']!='fdipzone' || $_SERVER['PHP_AUTH_PW']!='123456') {    header('WWW-Authenticate: Basic realm="NMG Terry"');    header('HTTP/1.0 401 Unauthorized');    echo "You must enter a valid login ID and password to access this resource.\n";    exit();}require 'SOAPHandle.class.php';$config = array(            'uri' => 'http://demo.fdipzone.com/soap/server.php');$oHandle = new SOAPHandle;// no wsdl modetry{    $server = new SOAPServer(null, $config);    $server->setObject($oHandle);    $server->handle();}catch(SOAPFault $f){    echo $f->faultString;}?>
client.php

<?php$config = array(            'location' => 'http://demo.fdipzone.com/soap/server.php',            'uri' => 'http://demo.fdipzone.com/soap/server.php',            'login' => 'fdipzone',            'password' => '123456',            'trace' => true);try{    $auth = array('fdipzone', '654321');    // no wsdl    $client = new SOAPClient(null, $config);    $header = new SOAPHeader('http://demo.fdipzone.com/soap/server.php', 'auth', $auth, false, SOAP_ACTOR_NEXT);    $client->__setSoapHeaders(array($header));    $revstring = $client->revstring('123456');    $strtolink = $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone', 'fdipzone blog', 1));    $uppcase = $client->__soapCall('uppcase', array('Hello World'));    echo $revstring.'<br>';    echo $strtolink.'<br>';    echo $uppcase.'<br>';}catch(SOAPFault $e){    echo $e->getMessage();}?>
SOAPHandle.class.php

<?phpclass SOAPHandle{ // class start    // header 驗證    public function auth($auth){        if($auth->string[0]!='fdipzone' || $auth->string[1]!='654321'){            throw new SOAPFault('Server', 'No Permission');        }    }// 反轉字符串    public function revstring($str=''){        return strrev($str);    }    // 字符傳轉連接    public function strtolink($str='', $name='', $openwin=0){        $name = $name==''? $str : $name;        $openwin_tag = $openwin==1? ' target="_blank" ' : '';        return sprintf('<a href="%s" %s>%s</a>', $str, $openwin_tag, $name);    }    // 字符串轉大寫    public function uppcase($str){        return strtoupper($str);    }} // class end?>

SOAPHeader 第四与第五个参数说明:

Must Understand

这个参数指明了, 是否服务端必须要响应SoapHeader, 如果这个参数为真, 而服务端并不能识别响应的Header,则会引发一个Soap Fault(Header not understood)。

SOAP_ACTOR_NEXT

actor指明了SoapHeader要传递给谁, 被哪个Service处理。

SOAP_ACTOR_NEXT的意思就是, 下一个接受到这个请求头的Service。

在SoapServer的构造函数中, 我们可以指明一个Server的Actor, 比如:

<?php$config = array(            'uri' => 'http://demo.fdipzone.com/soap/server.php',            'actor' => 'myserver');$server = new SOAPServer(null, $config);?>
然后就可以在Client的SoapHeader中, 通过设置actor是myserver, 来让指定的Server来获得我们设置的头部的信息。

源码下载地址:点击查看



1 0