Home  >  Article  >  php教程  >  WebService-php- 1(16),webservice-php-16

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

WBOY
WBOYOriginal
2016-06-13 09:05:25947browse

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

string string;
}

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

返回结果

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

3 搭建WebService服务器

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

<?xml version =<span>'</span><span>1.0</span><span>'</span> encoding =<span>'</span><span>UTF-8</span><span>'</span> ?>
<definitions name=<span>'</span><span>自定义名称[可选]</span><span>'</span><span>
targetNamespace</span>=<span>'</span><span>命名空间[一般为URL]</span><span>'</span><span>
xmlns:tns</span>=<span>'</span><span>命名空间[值同targetNamespace]</span><span>'</span><span>
xmlns:soap</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/soap/</span><span>'</span><span>
xmlns:xsd</span>=<span>'</span><span>http://www.w3.org/2001/XMLSchema</span><span>'</span><span>
xmlns:soapenc</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span><span>
xmlns:wsdl</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/</span><span>'</span><span>
xmlns</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/</span><span>'</span>>
<!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型-->
<types>
<xsd:schema xmlns:xsd=<span>"</span><span>http://www.w3.org/2001/XMLSchema</span><span>"</span><span>
targetNamespace</span>=<span>"</span><span>[值同上tns]</span><span>"</span>>
</xsd:schema>
</types>
<!--
<message><span> 元素可定义每个消息的部件,以及相关联的数据类型.
</span>-->
<message name=<span>'</span><span>操作名Request</span><span>'</span>>
<part name=<span>"</span><span>term</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/>
</message>
<message name=<span>'</span><span>操作名Response</span><span>'</span>>
<part name=<span>"</span><span>value</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/>
</message>
<!--
<portType><span> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息.
它告诉你去哪个WebService的连接点,扮演了一个控制者.
</span>-->
<portType name=<span>'</span><span>操作列表名</span><span>'</span>>
<operation name=<span>'</span><span>操作名</span><span>'</span>>
<input message=<span>'</span><span>tns:操作名Request</span><span>'</span>/>
<output message=<span>'</span><span>tns:操作名Response</span><span>'</span>/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name=<span>'</span><span>WS下的频道名称</span><span>'</span> type=<span>'</span><span>tns:频道下的操作列表</span><span>'</span>>
<!--style:属性可取值 <span>"</span><span>rpc</span><span>"</span> 或 <span>"</span><span>document</span><span>"</span>,ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP-->
<soap:binding style=<span>'</span><span>rpc</span><span>'</span><span>
transport</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/http</span><span>'</span>/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<operation name=<span>'</span><span>test</span><span>'</span>>
<soap:operation soapAction=<span>'</span><span>http://www.cwtservice.cn/newOperation/</span><span>'</span>/>
<input>
<soap:body use=<span>'</span><span>encoded</span><span>'</span> <span>namespace</span>=<span>'</span><span>urn:xmethods-delayed-quotes</span><span>'</span><span>
encodingStyle</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span>/>
</input>
<output>
<soap:body use=<span>'</span><span>encoded</span><span>'</span> <span>namespace</span>=<span>'</span><span>urn:xmethods-delayed-quotes</span><span>'</span><span>
encodingStyle</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span>/>
</output>
</operation>
</binding>
<!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
<service name=<span>'</span><span>WebService名称[如weatherWS,shopWS]</span><span>'</span>>
<port name=<span>'</span><span>WS下的频道名称[如cartSoap,购物车服务]</span><span>'</span> binding=<span>'</span><span>tns:[频道名,同左]</span><span>'</span>>
<soap:address location=<span>'</span><span>http://[webservice地址]</span><span>'</span>/>
</port>
</service>
</definitions>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn