码迷,mamicode.com
首页 > Web开发 > 详细

php soap实现WebService接口

时间:2014-11-14 17:12:30      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   使用   sp   文件   数据   

nusoap是php写的一个功能文件,下载地址:http://pan.baidu.com/s/1i3mUQJr

一、不使用wsdl
服务端 server.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);  
?>  

客户端 client.php

<?php  
	//包函nusoap.php  
	require_once(‘./lib/nusoap.php‘);  
	//新建一个soap客户端,调用服务端提供的wsdl  
	$client = new soapclient(‘http://localhost/test/server.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>‘; 	 
?> 


二、使用wsld
服务端 serve.php

<?php  
	//包函nusoap.php  
	require_once(‘./lib/nusoap.php‘);  
	//新建一个soap服务  
	$server = new soap_server();  
	$server->soap_defencoding = ‘UTF-8‘;
	//$ser->decode_utf8 = false;
	$server->xml_encoding = ‘UTF-8‘;
	//初始化支持wsdl  
	$server->configureWSDL(‘hellowsdl2‘, ‘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);  
?> 

服务端 client.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>‘;  
	}  
	//要向服务端要传的参数  
	$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 soap实现WebService接口

标签:blog   http   io   ar   os   使用   sp   文件   数据   

原文地址:http://www.cnblogs.com/rnckty/p/4097500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!