php webservice

时间:2024-04-28 08:04:53

发请求客户端client.php

<?php
//需要到php.ini文件中打开extension=php_soap.dll
try{
//wsdl方式调用web service
//wsdl方式中由于wsdl文件写定了,如果发生添加删除函数等操作改动,不会反应到wsdl,相对non-wsdl方式来说不够灵活
//$soap = new SoapClient("http://127.0.0.1/test/test.wsdl"); //non-wsdl方式调用web service,没有要调用的wsdl文件,所以第一参数为空
//location webservice服务路径的地址
//uri 要用要调用的webserivce中的uri一致
$soap = new SoapClient(null, array('location'=>'http://127.0.0.1/test/server.php', 'uri' =>'test_php_webservice')); echo $soap->fun1(array(1,2,3));
//echo $soap->fun2(123); //wsdl方式wsdl文件中未定义fun3,fun4方法,所以此处只能以non-wsdl方式调用
//echo $soap->fun3('sss');
//echo $soap->fun4('aaa');
}catch(SoapFault $fault){
//可以使用try catch也可以不用,使用try catch错误信息会自动记录到SoapFault类对象中
echo $fault->getMessage();;
}catch(Exception $e){
echo $e->getMessage();
} /*
SoapClient类 这个类用来使用Web services。SoapClient类可以作为给定Web services的客户端。
它有两种操作形式: * WSDL 模式
* Non-WSDL 模式 在WSDL模式中,构造器可以使用WSDL文件名作为参数,并从WSDL中提取服务所使用的信息。 non-WSDL模式中使用参数来传递要使用的信息。 SoapServer类 这个类可以用来提供Web services。与SoapClient类似,SoapServer也有两种操作模式:WSDL模式和non-WSDL模式。这两种模式的意义跟 SoapClient的两种模式一样。在WSDL模式中,服务实现了WSDL提供的接口;在non-WSDL模式中,参数被用来管理服务的行为。 在SoapServer类的众多方法中,有三个方法比较重要。它们是SoapServer::setClass(),SoapServer::addFunction()和SoapServer::handle()。
*/ //==============================实战链接xml===================================================== //假如给定了一个xml请求和响应示例,要按此规范向服务器发送请求及接收返回信息
//以下是 SOAP 1.2 xml请求示例。如发送请求的内容也是xml,则将占位符(string)替换为实际值即可。
/*
POST /ws_member.asmx HTTP/1.1
Host: 141.207.55.190
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UpdatePassword xmlns="http://tempuri.org/">
<strCallUserCode>string</strCallUserCode>
<strCallPassword>string</strCallPassword>
<strMemberCode>string</strMemberCode>
<strOrgPassword>string</strOrgPassword>
<strNewPassword>string</strNewPassword>
</UpdatePassword>
</soap12:Body>
</soap12:Envelope>
*/ //以下是 SOAP 1.2 响应示例,即请求后服务器返回信息内容
/*
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UpdatePasswordResponse xmlns="http://tempuri.org/">
<UpdatePasswordResult>string</UpdatePasswordResult>
</UpdatePasswordResponse>
</soap12:Body>
</soap12:Envelope>
*/ //将更改占位符后的xml保存到变量$xmldata中
$xmldata = <<<EOT
POST /ws_member.asmx HTTP/1.1
Host: 141.207.55.190
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<UpdatePassword xmlns="http://tempuri.org/">
<strCallUserCode>TEST</strCallUserCode>
<strCallPassword>123456</strCallPassword>
<strMemberCode>01201201103</strMemberCode>
<strOrgPassword>1234</strOrgPassword>
<strNewPassword>123</strNewPassword>
</UpdatePassword>
</soap12:Body>
</soap12:Envelope>
EOT; //wsdl地址
$wsdl = 'http://141.207.55.190:17654/ws_member.asmx?WSDL';
try{
$client = new SoapClient($wsdl);
//var_dump ( $client->__getTypes() );//获取服务器上数据类型
//var_dump ( $client->__getFunctions() );//获取服务器上提供的方法 //方法一,发送xml请求(这里未成功连接获取信息)
//发送xml必须使用__doRequest,
//$result = $client->__doRequest($xmldata,$wsdl,'UpdatePassword',2);
//print_r($result);
//exit; //方法二,根据xml格式获取请求方法名,及将xml数据转成对应格式数组,传递信息,实测成功
/*
$data = array();
$data['strCallUserCode'] = 'TEST';
$data['strCallPassword'] = '123456';
$data['strMemberCode'] = '01201201103';
$data['strOrgPassword'] = '1234';
$data['strNewPassword'] = '123';
echo '修改密码:';
$result = $client->UpdatePassword($data);
var_dump($result);echo "<br/><br/>";
exit;
*/
}catch (SoapFault $e){
echo $e->getMessage();
}catch(Exception $e){
echo $e->getMessage();
}
?>

服务器端server.php

<?php
class TestClass {
function fun1($arg1) {
return json_encode($arg1);
}
function fun2($arg2) {
return $arg2;
}
}
function fun3($arg3) {
return $arg3;
}
function fun4($arg4) {
return $arg4;
} //wsdl方式提供web service,如果生成了wsdl文件则可直接传递到//SoapServer的构造函数中
//ini_set('soap.wsdl_cache_enabled','0');//关闭WSDL缓存
//$server = new SoapServer('http://127.0.0.1/test/test.wsdl');
/*
已有现成的简单wsdl文件,需要使用时可在此基础上简单修改使用
1,把所有test_php_webservice换成自定义名称
2,把类名TestClass换成实际所需类名
3,把方法名fun1,fun2换成实际所用方法名,及参数,返回值类型,也可按已有fun1,fun2标签格式新增所需方法
4,把http://127.0.0.1:80/test/server.php换成实际服务器端处理路径
*/ //non-wsdl方式这个是没有使用wsdl文件的,所以第一个参数为null,如果有使用wsdl,那么第一个参数就是这个wsdl文件的地址。
//uri 相当于命名空间,可以是任何不和别人重合的字符串
//soap_version 表示soap的版本号,目前就两个版本号SOAP_1_1,SOAP_1_2
$server = new SoapServer ( null, array ( 'uri' => 'test_php_webservice', 'soap_version' => SOAP_1_2 ) ); // setClass和addFunction不能同时设置,addFunction可以添加多个方法,设置的setClass类里面的方法或者addFunction添加的方法,可以在客户端直接通过$soap对象->方法名调用
$server->setClass ( 'TestClass' ); //addFunction只适用于non-wsdl方式,wsdl方式可访问方法都是在wsdl中固定的
//$server->addFunction('fun3');
//$server->addFunction('fun4');
$server->handle ();
?>

test.wsdl文件

<?xml version="1.0" ?>
<definitions name="test_php_webservice" targetNamespace="urn:test_php_webservice" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:test_php_webservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types xmlns="http://schemas.xmlsoap.org/wsdl/" /> <portType name="test_php_webservicePort"> <operation name="fun1">
<input message="tns:fun1Request" />
<output message="tns:fun1Response" />
</operation> <operation name="fun2">
<input message="tns:fun2Request" />
<output message="tns:fun2Response" />
</operation> </portType> <binding name="test_php_webserviceBinding" type="tns:test_php_webservicePort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> <operation name="fun1">
<soap:operation soapAction="urn:test_php_webservice#TestClass#fun1" />
<input>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation> <operation name="fun2">
<soap:operation soapAction="urn:test_php_webservice#TestClass#fun2" />
<input>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:test_php_webservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation> </binding>
<service name="test_php_webservice">
<documentation />
<port name="test_php_webservicePort" binding="tns:test_php_webserviceBinding"><soap:address location="http://127.0.0.1:80/test/server.php" />
</port>
</service> <message name="fun1Request">
<part name="name" type="xsd:array" />
</message>
<message name="fun1Response">
<part name="fun1" type="xsd:array" />
</message> <message name="fun2Request">
<part name="name" type="xsd:array" />
</message>
<message name="fun2Response">
<part name="fun2" type="xsd:string" />
</message> </definitions>