WebService-php- 1(16),webservice-php-16

原创
2016-06-13 09:05:25 681浏览

WebService-php- 1(16),webservice-php-16

最近看了挺多关于php中webservice的资料,感谢燕十八的分享,帮助了我构建服务端的过程。将学习笔记记录如下,其中包含燕十八的笔记。

WebService

1 快速了解WebService

通俗的说:按一定的XML格式,调用远程服务器的方法,且服务器按一定的格式返回XML内容.
"一定的格式"----SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协
议.
远程服务器 ---- 一般通过HTTP协议来传递消息
总结: WebServie == HTTP协议 + Soap格式的XML

例1:soap请求

  POST /WebServices/MobileCodeWS.asmx HTTP/1.1
  Host: webservice.webxml.com.cn
  Content-Type: text/xml; charset=utf-8
  Content-Length: 354
  SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
  
  

2 PHP客户端请求WebService

  修改PHP.ini
  extension=php_soap.dll 前的";"去掉.
  并重启apache
  PHP SoapClient类可以用来请求WebService

$soap = new soapClient('http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL');
print_r($soap->getMobileCodeInfo( array('mobileCode'=>'13**********') ) );
Array
(
[0] => getMobileCodeInfoResponse getMobileCodeInfo(getMobileCodeInfo $parameters)
[1] => getDatabaseInfoResponse getDatabaseInfo(getDatabaseInfo $parameters)
)
Array
(
[0] => struct getMobileCodeInfo {
string mobileCode;
string userID;
}
[1] => struct getMobileCodeInfoResponse {
string getMobileCodeInfoResult;
}
[2] => struct getDatabaseInfo {
}
[3] => struct getDatabaseInfoResponse {
ArrayOfString getDatabaseInfoResult;
}
[4] => struct ArrayOfString {

string string;
}

// 调用方法
print_r($soap->getMobileCodeInfo( array('mobileCode'=>'13426060134') ) );

返回结果

stdClass Object ( [getMobileCodeInfoResult] => 13*********:北京 北京 北京移动动感地带卡 )

3 搭建WebService服务器

wsdl是什么?
wsdl是WebService的规格说明书.

'1.0' encoding ='UTF-8' ?>
'自定义名称[可选]'
targetNamespace='命名空间[一般为URL]'
xmlns:tns='命名空间[值同targetNamespace]'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>

"http://www.w3.org/2001/XMLSchema"
targetNamespace="[值同上tns]">
'操作名Request'>
"term" type="xsd:string"/>
'操作名Response'>
"value" type="xsd:string"/>
'操作列表名'>
'操作名'>
'tns:操作名Request'/>
'tns:操作名Response'/>
'WS下的频道名称' type='tns:频道下的操作列表'>

'rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>

'test'>
'http://www.cwtservice.cn/newOperation/'/>
'encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>

'encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
'WebService名称[如weatherWS,shopWS]'>
'WS下的频道名称[如cartSoap,购物车服务]' binding='tns:[频道名,同左]'>
'http://[webservice地址]'/>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。