代码地址
git clone https://gitee.com/yanglingyun/webservice_learn.git
概念
- 以xml形式交换数据
php实现
php.ini开启soap
php7.3.4
extension=soap
测试调用
<?php
// 利用soapclient
$soapclient = new SoapClient('http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl');
// 客户端通过wsdl,了解webservice可调用方法与参数细节
// print_r($soapclient->__getFunctions());// 获得调用方法
print_r($soapclient->getMobileCodeInfo(array('mobileCode'=>'13426060134')));
获得调用方法
得
Array ( [0] => getRegionDatasetResponse getRegionDataset(getRegionDataset $parameters) [1] => getRegionProvinceResponse getRegionProvince(getRegionProvince $parameters) [2] => getRegionCountryResponse getRegionCountry(getRegionCountry $parameters) [3] => getSupportCityDatasetResponse getSupportCityDataset(getSupportCityDataset $parameters) [4] => getSupportCityStringResponse getSupportCityString(getSupportCityString $parameters) [5] => getWeatherResponse getWeather(getWeather $parameters) [6] => getRegionDatasetResponse getRegionDataset(getRegionDataset $parameters) [7] => getRegionProvinceResponse getRegionProvince(getRegionProvince $parameters) [8] => getRegionCountryResponse getRegionCountry(getRegionCountry $parameters) [9] => getSupportCityDatasetResponse getSupportCityDataset(getSupportCityDataset $parameters) [10] => getSupportCityStringResponse getSupportCityString(getSupportCityString $parameters) [11] => getWeatherResponse getWeather(getWeather $parameters) )
数组传参调用
print_r($soapclient->getMobileCodeInfo(array('mobileCode'=>'13426060134')));
搭建webservice服务
WSDL服务描述文档
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="HelloWorldService" targetNamespace="http://soap.com" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xs:schema targetNamespace="http://soap.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
</xs:schema>
</wsdl:types>
<!-- add的消息(相当于类有一个add方法) -->
<wsdl:message name="sumReq">
<!-- 消息里面包含add对象(相当于类中要传入的值) -->
<wsdl:part type="xsd:string" name="item" />
</wsdl:message>
<!-- add的返回消息相当于return -->
<wsdl:message name="sumRes">
<wsdl:part type="xsd:string" name="value" />
</wsdl:message>
<wsdl:portType name="oplist">
<wsdl:operation name="sum">
<wsdl:input message="tns::sumReq"/>
<wsdl:output message="tns::sumRes"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="cartWsBi" type="tns:oplist">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sum">
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="shop">
<wsdl:port binding="tns:cartWsBi" name="cartWs">
<soap:address location="http://soap.com/service.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
处理函数service.php
<?php
function sum($str){
return 'webservice' . $str;
}
$server = new soapServer('http://soap.com/wsdl.xml');
$server->addFunction('sum');// 注册
$server->handle();
请求文件client.php
<?php
// 利用soapclient
$soapclient = new SoapClient('http://soap.com/wsdl.xml');
// 客户端通过wsdl,了解webservice可调用方法与参数细节
// print_r($soapclient->__getFunctions());// 获得调用方法
print_r($soapclient->sum('13426060134'));
搭建xml_rpc服务
简化soap,对参数包装简单
php.ini开启php_xmlrpc
extension=xmlrpc
创建rpc服务器
<?php
function hello($str){
return 'hello world'.$str;
}
$server = xmlrpc_server_create();
// 注册用户函数入服务器 参数: 服务器 注册函数名 注册后函数名
xmlrpc_server_register_method($server,'hello','hello');
// 收取POST请求 内容为xml
$req = $HTTP_RAW_POST_DATA;
// 响应用户
$res = xmlrpc_server_call_method($server, $req, null);
// 输出
header('content-type:text/xml');
echo $res;
// 销毁服务器
xmlrpc_server_destroy($server);
rpc请求
<?php
class rpcclient {
protected $url;
public function __construct($url)
{
$this->url = $url;
}
protected function __query($req){
$context = stream_context_create(
array(
'http'=>array(
'method'=>'POST',//大写必须
'header'=>'content-type:text/xml',
'content'=>$req
)
)
);
return xmlrpc_decode(file_get_contents($this->url, false,$context));
}
public function __call($method, $args)
{
$req = xmlrpc_encode_request($method, $args);
return $this->__query($req);
}
}
$client = new rpcclient('http://soap.com/rpc-server.php');
print_r($client->hello());