最近看了挺多關於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㜀 集ml .com.cn/getMobileCodeInfo"
extension=php_soap.dll 前的";"去掉.
並重啟apache PHP SoapClient類別可以用來請求WebService
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>