您的位置:首页 > 编程语言 > PHP开发

php soap实例讲解

2017-04-19 16:28 543 查看
一,什么是soap,什么是wsdl,为什么要用他们

SOAP是基于XML和HTTP通信协议,xml各种平台,各种语言都支持的一个种语言。http呢它得到了所有的因特网浏览器及服务器的支持。

WSDL 指网络服务描述语言 (Web Services Description Language),是一种使用 XML 编写的文档。这种文档可描述某个 Web service。它可规定服务的位置,以及此服务提供的操作。

我是做php的,你是java的,他是做.net,如果我们三个之间要进行通信,要进行数据交换,怎么办呢?我们需要一个能和我们都能通信的工具。soap,wsdl被创造出来,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

二,实例

如果php要使用soap的话,通常做法是,添加了一下php的soap模块,在php.ini里面加上soap.so,下面介绍一个不要添加soap.so文件,也可以实现soap的方法

nusoap是php写的一个功能文件,包涵进来就可以用了,网上很多自己去搜一下吧。

1,不使用wsdl

a,服务端helloworld2.php

查看复制打印?

<?php

//包函nusoap.php

require_once('./lib/nusoap.php');

//创建服务端

$server = new soap_server;

//定义客户端调用方法

$server->register('hello');

//调用方法以及参数

function hello($name) {

return 'Hello, ' . $name;

}

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

$server->service($HTTP_RAW_POST_DATA);

?>

b,客户端hello.php

查看复制打印?

<?php

//包函nusoap.php

require_once('./lib/nusoap.php');

//新建一个soap客户端,调用服务端提供的wsdl

//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);

$client = new soapclient('http://localhost/test/helloworld2.php');

//查看一下是不是报错

$err = $client->getError();

if ($err) {

//显示错误

echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';

}

//调用服务端的方法

$result = $client->call('hello', array('person' => "this is a test"));

echo '<h2>Result</h2><pre>';

print_r($result);

echo '</pre>';

?>

2,使用wsld

a,服务器端

查看复制打印?

<?php

//包函nusoap.php

require_once('./lib/nusoap.php');

//新建一个soap服务

$server = new soap_server();

//初始化支持wsdl

$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');

//定义数据结构来接收数据

$server->wsdl->addComplexType(

'Person',

'complexType',

'struct',

'all',

'',

array(

'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),//后面的type定义数据的类型,这个是string

'age' => array('name' => 'age', 'type' => 'xsd:int'),//后面的type定义数据的类型,这个是int

'gender' => array('name' => 'gender', 'type' => 'xsd:string')//后面的type定义数据的类型,这个是string

)

);

$server->wsdl->addComplexType(

'SweepstakesGreeting',

'complexType',

'struct',

'all',

'',

array(

'greeting' => array('name' => 'greeting', 'type' => 'xsd:string'),

'winner' => array('name' => 'winner', 'type' => 'xsd:string')

)

);

//服务器定义的soap调用方法

$server->register('hello', // 方法名字hello,方法就在下面

array('person' => 'tns:Person'), // 客户端传来的变量

array('return' => 'tns:SweepstakesGreeting'), //返回参数

'urn:hellowsdl2', // soap名

'urn:hellowsdl2#hello', // soap的方法名

'rpc', // 使用的方式

'encoded', // 编码

'test' // 存档

);

//定义上面注册过的函数hello

function hello($person) {

$greeting = 'Hello, ' . $person['firstname'] .

'. It is nice to meet a ' . $person['age'] .

' year old ' . $person['gender'] . '.';

$winner = 'Scott';

//要返回的数据

return array(

'greeting' => $greeting,

'winner' => $winner

);

}

// 请求时(试图)调用服务

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

$server->service($HTTP_RAW_POST_DATA);

?>

b,客户端

查看复制打印?

<?php

//包函nusoap.php

require_once('./lib/nusoap.php');

//新建一个soap客户端,调用服务端提供的wsdl

//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);

$client = new soapclient('http://localhost/test/helloworld2.php');

//查看一下是不是报错

$err = $client->getError();

if ($err) {

//显示错误

echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';

}

//要向服务端要传的参数

$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');

//调用服务端的方法

$result = $client->call('hello', array('person' => $person));

//错误审核

if ($client->fault) {

echo '<h2>Fault</h2><pre>';

print_r($result);

echo '</pre>';

} else {

$err = $client->getError();

if ($err) {

echo '<h2>Error</h2><pre>' . $err . '</pre>';

} else {

echo '<h2>Result</h2><pre>';

print_r($result);

echo '</pre>';

}

}

//显示请求信息

echo '<h2>Request</h2>';

echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';

//显示返回信息

echo '<h2>Response</h2>';

echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

//显示调试信息

echo '<h2>Debug</h2>';

echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';

?>

上面二个例子不管是客户端,还是服务器端,都是用php写的,你可以试着用多种语言来写,来测试一下。不管你是用php的模块,还是用nusoap,里面具体方法就不在这多说了,手册里面都有。

42
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: