WebService-php-1 (16), webservice-php-16
Recently I have read a lot of documents about webservice in php. Thanks to the sharing of Yan 18, it helped me build the server. The learning notes are recorded as follows, including the notes of Yan 18.
WebService
1. Quick introduction to WebService
In general, the remote server method is called in a certain XML format, and the server returns XML content in a certain format.
"A certain format" ---- SOAP (Simple Object Access Protocol) Simple Object Access Protocol is a Simple Protocol for exchanging information in a distributed or distributed environment. It is an XML-based Protocol.
Discussion.
Remote Server-messages are generally transmitted over HTTP.
Conclusion: WebServie = XML in HTTP + 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"
<? Xml version = "1.0" encoding = "UTF-8"?>
<Soap: Envelope xmlns: xsi = "The http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "The http://www.w3.org/2001/XMLSchema" xmlns: soap = "The http://WebXml.com.cn/getMobileCodeInfo>
2 PHP client request WebService
Modify PHP. ini
Extension = ";" removed before php_soap.dll.
And restart apache
PHP SoapClient class can be used to request 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;
}
// Call method print_r ($ soap-> getMobileCodeInfo (array ('lelecode' => '20140901 ')));
Returned results
StdClass Object ([getMobileCodeInfoResult] => 13 **********: Beijing Mobile Phone motion zone card)
3. Construct a WebService Server
What is wsdl?
Wsdl is the specification of WebService.
<? Xml version = '1. 0' encoding = 'utf-8'?> <Definitions name = 'custom name [Optional] 'targetnamespace = 'namespace [generally URL] 'xmlns: tns = 'namespace [value the same as targetNamespace]' xmlns: soap = 'HTTP: // response> <! -- <Types> The element defines the data type used by the web service. The WSDL uses the XML Schema syntax to define the data type. You can also customize the types not included in the Schema --> <types> <xsd: schema xmlns: xsd = "http://www.w3.org/2001/XMLSchema" targetNamespace = "[same value as tns]"> </xsd: schema> </types> <! The <message> element defines the components of each message and the associated data types. --> <message name = 'Operation name request'> <part name = "term" type = "xsd: string "/> </message> <message name = 'Operation name response'> <part name =" value "type =" xsd: string "/> </message> <! -- <PortType> elements are the most important WSDL elements. it describes a web service, executable operations, and related messages. it tells you which WebService connection point you are going to act as a controller. --> <portType name = 'Operation list name'> <operation name = 'Operation name'> <input message = 'tns: operation name Request '/> <output message = 'tns: operation name Response'/> </operation> </portType> <! -- <Binding> The element defines the Message format and protocol details for each port --> <binding name = 'ws channel name 'Type = 'tns: operation list under the channel '> <! -- Style: the attribute can be "rpc" or "document". ransport: the attribute defines the SOAP protocol to be used. in this example, we use HTTP --> <soap: binding style = 'rpc 'transport = 'HTTP: // schemas.xmlsoap.org/soap/http'/> <! The -- operation element defines the operators provided by each port. For each operation, the corresponding SOAP behavior needs to be defined --> <operation name = 'test'> <soap: operation soapAction = 'HTTP: // www.cwtservice.cn/newoperation/'/> <input> <soap: body use = 'encoded' namespace = 'urn: xmethods-delayed-quotes 'encodingStyle = 'HTTP: // outputs> </input> <output> <soap: body use = 'encoded' namespace = 'urn: xmethods-delayed-quotes 'encodingStyle = 'HTTP: // schemas.xmlsoap.org/so Ap/encoding/'/> </output> </operation> </binding> <! -- <Service> contains one or more port elements. Each port element represents a different Web service --> <service name = 'webservice name [such as weatherWS, shopWS] '> <port name = 'ws channel name [such as cartSoap, shopping cart Service] 'binding = 'tns: [channel name, same as left]'> <soap: address location = 'HTTP: // [webservice address] '/> </port> </service> </definitions>