php soap mysql_php soap 使用实例

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

phpize

./configure

sudo make

sudo make test安装成功后在phpinfo可以看到soap扩展

8228f7ec47508029fddf2b143541bf2e.png

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

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

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

SOAP中主要用到三个类,SOAPServer,SOAPClient,SOAPFault

NO-WSDL模式:

soapHandle.class.php 处理请求

class soapHandle{

public function strtolink($url=''){

return sprintf('%s', $url, $url);

}

}

?>server.php soap服务端

// 服务器验证

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"); // 处理请求的class

try{

$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客户端

try{

$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').'
'; // 直接调用server方法

echo $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone')); // 间接调用server方法

}catch(SOAPFault $e){

echo $e->getMessage();

}

?>

Header验证例子:

server.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 mode

try{

$server = new SOAPServer(null, $config);

$server->setObject($oHandle);

$server->handle();

}catch(SOAPFault $f){

echo $f->faultString;

}

?>client.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.'
';

echo $strtolink.'
';

echo $uppcase.'
';

}catch(SOAPFault $e){

echo $e->getMessage();

}

?>SOAPHandle.class.php

class 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('%s', $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, 比如:

$config = array(

'uri' => 'http://demo.fdipzone.com/soap/server.php',

'actor' => 'myserver'

);

$server = new SOAPServer(null, $config);

?>然后就可以在Client的SoapHeader中, 通过设置actor是myserver, 来让指定的Server来获得我们设置的头部的信息。

源码下载地址:点击查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值