您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

WebService最常用的两种方法

2025/11/26 17:19:54发布24次查看
企业级应用,主要是讲php5对webservice的一些实现(以下的程序可以被java,net,c等正常调用)国内用php写webservice的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考
客户端
代码:
01. 1, 'uri' => 'http://www.zxsv.com/soapdiscovery/' ) );
08./*
09.* 获取soapclient对象引用的服务所提供的所有方法
10.*/
11.echo (soap服务器提供的开放函数:);
12.echo ('
');
13.var_dump ( $client->__getfunctions () );
14.echo ('
');
15.echo (soap服务器提供的type:);
16.echo ('');
17.var_dump ( $client->__gettypes () );
18.echo ('
');
19.echo (执行getguidnode的结果:);
20.//$users = $client->getusers();
21.//var_dump($helloworld );
22.$parameters = array('uname'=>'zxsv',upassword=>'123');
23.$out = $client->helloworld($parameters);
24.$datadb = $out->helloworldresponse;
25.var_dump($out);
26.?>服务端
代码:
01.userid = $parmas[0];
08.$this->name = $parmas[1];
09.}
10.}
11.$servidorsoap = new soapserver('testphp.xml',array('uri' => 'http://www.testphp.com/','encoding'=>'utf-8','soap_version' => soap_1_2 ));
12.$servidorsoap->setclass(testphp);
13.$servidorsoap->handle();
14.class testphp {
15.public function helloworld($uid){
16.return array('helloworldresult'=>mystring.$uid->{'uname'}.' and '.$uid->{'upassword'});
17.}
18.public function getmember($uid){
19.$s=array();
20.for($i=0;$i{'uid'};$i++){
21.$s[] =&new member(array($i, $uid->{'uname'}.'我测试'.$i));
22.}
23.return   array('getmemberresult'=>$s);
24.}
25.}
26.?>
到这里应该都看的懂吧
下面是wsdl文件
代码:
001.
002.
003.
004.
005.
006.
007.
008.
009.
010.
011.
012.
013.
014.
015.
016.
017.
018.
019.
020.
021.
022.
023.
024.
025.
026.
027.
028.
029.
030.
031.
032.
033.
034.
035.
036.
037.
038.
039.
040.
041.
042.
043.
044.
045.
046.
047.
048.
049.
050.
051.
052.
053.
054.
055.
056.
057.
058.
059.
060.
061.
062.
063.
064.
065.
066.
067.
068.
069.
070.
071.
072.
073.
074.
077.
078.
079.
080.
081.
082.
083.
086.
087.
088.
089.
090.
091.
092.
093.
094.
095.
098.
099.
100.
101.
102.
103.
104.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
这里有返回的两个字段,一个是返回字符串,这个很好理解
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
这一段就字符串的
那返回数组的就比较麻烦了,我和老农搞了一两周才发现是wsdl文件写错了,看下面的一段
01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
第一段getmember是输入,最重要的是getmemberresponse这段,看type=”tns:arrayofmember”这里,返回一 个数组,wsdl中定义了arrayof这个,后面的就简单了,arrayofmember的类型是type=”tns:member” ,从name=”member”得到要返回的数组,完工。
ping service,博客程序提供一种通知机制,以便在第一时间将博客的更新信息发布到提供ping service服务的网站,写聚合的时候研究了一下
先看标准吧
这是一个标准的ping service,用xmlrpc来传数据的,注释写的这么详细,代码说明就不需要了吧,php5开启xmlrpc方法
client.php
代码:
01.
server.php
代码:
01.false,'message'=>'thanks for the ping.');
15. 
return $xml_rpc_string;
16.}
17.function rpc_server_ping($method, $params) {
18.   
$title = $params[0];
19.   
$server = $params[1];
20.       
//中间的判断,成功返回$xml_rpc_string
21.   
$xml_rpc_string = array('flerror'=>false,'message'=>'thanks for the ping.');
22. 
return $xml_rpc_string;
23.}
24.//产生一个xml-rpc的服务器端
25.$xmlrpc_server = xmlrpc_server_create();
26.//注册一个服务器端调用的方法rpc_server,实际指向的是rpc_server_extendedping函数
27.xmlrpc_server_register_method($xmlrpc_server, weblogupdates.extendedping, rpc_server_extendedping);
28.xmlrpc_server_register_method($xmlrpc_server, weblogupdates.ping, rpc_server_ping);
29.//接受客户端post过来的xml数据
30.$request = $http_raw_post_data;
31.//print_r($request);
32.//执行调用客户端的xml请求后获取执行结果
33.$xmlrpc_response = xmlrpc_server_call_method($xmlrpc_server, $request, null);
34.//把函数处理后的结果xml进行输出
35.header('content-type: text/xml');
36.echo $xmlrpc_response;
37.//销毁xml-rpc服务器端资源
38.xmlrpc_server_destroy($xmlrpc_server);
39.?>
类写的,有bug
代码:
01.xmlrpc_server = xmlrpc_server_create ();
09.       
$this->run ();
10.   
}
11.
12.   
//注册一个服务器端调用的方法rpc_server,实际指向的是ping函数
13.   
public function rpc_server() {
14.       
$this->methodname = !$this->methodname ? 'weblogupdates.extendedping':'weblogupdates.ping';
15.       
xmlrpc_server_register_method ( $this->xmlrpc_server, $this->methodname, array (__class__, ping));
16.   
}
17.       
/**
18.    
* 函数:提供给rpc客户端调用的函数
19.    
* 参数:
20.    
* $method 客户端需要调用的函数
21.    
* $params 客户端需要调用的函数的参数数组
22.    
* 返回:返回指定调用结果
23.    
*/
24.   
public function ping($method, $params) {
25.       
$this->title = $params [0];
26.       
$this->server = $params [1];
27.       
$this->rss = $params [2];
28.       
$this->tag = $params [3];
29.       
//$a  = $this->title ? $this->update():'';
30.       
$string = array ('flerror' => false, 'message' => 'thanks for the ping.', 'legal' => you agree that use of the blueidea.com ping service is governed by the terms of use found at www.blueidea.com. );
31.       
return $string;
32.   
}
33.
34.   
public function update(){
35.       
echo '这里放更新的一些条件';
36.   
}
37.
38.   
public function run() {
39.       
$this->rpc_server ();
40.       
$request = isset ( $globals [http_raw_post_data] ) ? file_get_contents ( php://input ) : $globals [http_raw_post_data];
41.       
$this->xmlrpc_response = xmlrpc_server_call_method ( $this->xmlrpc_server, $request, null );
42.       
//把函数处理后的结果xml进行输出
43.       
header ( 'content-type: text/xml' );
44.       
echo $this->xmlrpc_response;
45.   
}
46.
47.   
//销毁xml-rpc服务器端资源
48.   
public function __destruct() {
49.       
xmlrpc_server_destroy ( $this->xmlrpc_server );
50.   
}
51.}
52.$obj = new pings ( );
53.?>
webservice的最常用的两种方法算是写齐了
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product