WebService最常用的两种方法
企业级应用,主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVA,NET,C等正常调用)
国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)
客户端
代码:
01.<?php
02.header ( "Content-Type: text/html; charset=utf-8" );
03./*
04.* 指定WebService路径并初始化一个WebService客户端
05.*/
06.$ws = "http://soap/soapCspMessage.php?wsdl";
07.$client = new SoapClient ( $ws, array ('trace' => 1, 'uri' => 'http://www.zxsv.com/SoapDiscovery/' ) );
08./*
09.* 获取SoapClient对象引用的服务所提供的所有方法
10.*/
11.echo ("SOAP服务器提供的开放函数:");
12.echo ('<pre>');
13.var_dump ( $client->__getFunctions () );
14.echo ('</pre>');
15.echo ("SOAP服务器提供的Type:");
16.echo ('<pre>');
17.var_dump ( $client->__getTypes () );
18.echo ('</pre>');
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.<?php
02.class Member
03.{
04.public $UserId;
05.public $Name;
06.public function __construct($parmas){
07.$this->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->{'uid'};$i++){
21.$s[] =&new Member(array($i, $uid->{'uname'}.'我测试'.$i));
22.}
23.return array('GetMemberResult'=>$s);
24.}
25.}
26.?>
相关新闻>>
- 发表评论
-
- 最新评论 更多>>