I have read a lot of information about webservice in php recently, thank you Yan Shiba for sharing, it helped My process of building the server. Record your study notes as follows, including Yan Shiba’s notes.
WebService
1 Quickly understand WebService
In layman's terms: call the method of the remote server according to a certain XML format, and the server returns XML content in a certain format.
"Certain format"----SOAP (Simple Object Access Protocol) simple object The access protocol is a simple protocol for exchanging information in a decentralized or distributed environment. It is an XML-based protocol
.
Remote server ---- generally delivers messages through the HTTP protocol
Summary: WebServie == XML in HTTP protocol Soap format
Example 1: soap request
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"
="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" cn/getMobileCodeInfo >
Modify PHP.ini
Remove the ";" before extension=php_soap.dll.
And restart apache
The PHP SoapClient class can be used to request 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 {
}
<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>:北京 北京 北京移动动感地带卡 )
What is wsdl?
wsdl is the specification of 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>