Php soap server
It is very easy to create a SOAP server using PHP and nusoap. Basically, you only need to write the functions that you want to expose to your web services, and then register them with nusoap. OK. In addition, two steps are required to establish the PHP SOAP server. First, you needCodeCreate an instance of the nusoap object, and then use the http post method to transmit the original data to nusoap for processing.
Nusoap is easy to use. The most common classes are soap_server and soapclient, Soap_server is used to create a WebService, while soapclient is used to call WebService. . Both classes are defined in lib/nusoap. php. Therefore, we are creating or calling the WebService interface.ProgramThe file must be referenced.
Nusoap is a WebService programming tool in the PHP environment. It is used to create or call WebService. It is an open source software, is fully written in PHP language, through http to send and receive soap messages of a series of PHP class, developed by nusphere Corporation (http://dietrich.ganx4.com/nusoap. One advantage of nusoap is that it does not require the support of extension libraries. This feature allows nusoap to be used in all PHP environments and is not affected by server security settings.
1. First, go Http://sourceforge.net/projects/nusoap/ Download Nusoap.zip .
2. Server: Create Nusoapservice. php File.
soap_defencoding = 'utf-8'; $ server-> decode_utf8 = false; $ server-> xml_encoding = 'utf-8'; $ server-> configurewsdl ('test '); // open the WSDL support/* register the corresponding value of the program type to be accessed by the Client: bool-> "XSD: Boolean" string-> "XSD: string" int-> "XSD: INT "float->" XSD: float "*/$ server-> Register ('getteststr', // method name array (" name "=>" XSD: string "), // parameter. The default value is "XSD: St ". Ring "array (" return "=>" XSD: string "); // return value. The default value is" XSD: string "// is the isset check variable set to $ http_raw_post_data = isset ($ http_raw_post_data )? $ Http_raw_post_data: ''; // service processes data input from the client $ server-> service ($ http_raw_post_data ); /*** method for calling * @ Param $ name */function getteststr ($ name) {return "Hello, {$ name }! ";}?>
3. Client: Create Nusoapclient. php File.
<? Phprequire_once ("lib/nusoap. PHP ");/* call the address of the WSDL file of WebService parameter 1 through WSDL (the WSDL after the question mark cannot be capitalized) parameter 2 Specify whether to use WSDL $ client = new soapclient ('HTTP: // localhost/nusoapservice. PHP? WSDL ', true); */$ client = new soapclient ('HTTP: // localhost/nusoapservice. PHP '); $ client-> soap_defencoding = 'utf-8'; $ client-> decode_utf8 = false; $ client-> xml_encoding = 'utf-8 '; // when the parameter is converted into an array and $ paras = array ('name' => 'Bruce Lee ') is passed; // if the target method does not have a parameter, you can omit the following parameter $ result = $ client-> call ('getteststr', $ paras); // check the error and obtain the returned value if (! $ Err = $ client-> geterror () {echo "returned result:", $ result;} else {echo "call error:", $ err;}?>
<? Phprequire_once ("lib/nusoap. PHP ");/* call the address of the WSDL file of WebService parameter 1 through WSDL (the WSDL after the question mark cannot be capitalized) parameter 2 Specify whether to use WSDL $ client = new soapclient ('HTTP: // localhost/nusoapservice. PHP? WSDL ', true); */$ client = new soapclient ('HTTP: // localhost/nusoapservice. php? WSDL ', true); $ client-> soap_defencoding = 'utf-8'; $ client-> decode_utf8 = false; $ client-> xml_encoding = 'utf-8 '; // when the parameter is converted into an array and $ paras = array ('name' => 'Bruce Lee ') is passed; // if the target method does not have a parameter, the following parameters can be omitted: $ client-> call ('getteststr', $ paras); $ document = $ client-> document; echo $ document;?>
Note: Returned results: Hello, {Bruce Lee }!
WSDL
WSDL is an XML language used to describe Web Services. It is a machine read format that provides all the information required to access the service to the Web service client. Nusoap provides a class to parse wdsl files and extract information from them. Soapclient objects use the WSDL class to make it easier for developers to call services. With the help of WSDL information, the programmer can call a message by knowing the operation name and parameters.
Using WSDL through nusoap provides the following advantages:
All service meta files, such as namespaces, endpoint URLs, and parameter names, can be obtained directly from the WSDL file, this allows the client to dynamically adapt to changes on the server. Because the data can be obtained from the server at any time, you do not need to use hard encoding in the user script.
It allows us to use the soap_proxy class. This class is derived from the soapclient class and adds the methods corresponding to the operations listed in the wdsl file. Now you can directly call these methods through it.
The soapclient class contains a getproxy () PartyIt returns an object of the soap_proxy class. The soap_proxy class is derived from the soapclient class. It adds methods corresponding to the operations defined in the WSDL document and allows users to call an endpoint remote method. This is only applicable when the soapclient object is initialized using the wdsl file. The advantage is that it is easy for users to use. The disadvantage is that performance-creating objects in PHP is time-consuming and does not serve utilitarian purposes (and this functionality serves no utilitarian purpose ).
<? Phprequire_once ("lib/nusoap. php"); $ client = new soapclient ('HTTP: // localhost/nusoapservice. php? WSDL ', true); $ client-> soap_defencoding = 'utf-8'; $ client-> decode_utf8 = false; $ client-> xml_encoding = 'utf-8 '; // generate the proxy class $ proxy = $ client-> getproxy (); // call the remote function $ sq = $ proxy-> getteststr ('Bruce Lee '); If (! $ Err = $ proxy-> geterror () {print_r ($ sq);} else {print "error: $ Err";} print 'request: <XMP> '. $ p-> request. '</XMP>'; print 'response: <XMP> '. str_replace ('> <', "> \ n <", $ p-> response ). '</XMP>';?>
Step 4: run the Server File page: http: // localhost/
Nusoapservice . Php-generated WSDL File
Click the method name. By adding several lines of code to the service, we can use nusoap to provide a visual document for the service. However, this is not all we can do.
We add some WSDL calls to the service by using nusoap. We can generate the WSDL for the service and some other documents. The difference is that we can do less in the client, at least in our simple example. The client shown below is no different from the client that does not use WSDL. The only difference is that soapclent class is parsed by providing the wsdl url, instead of using the service endpoint.
Solution to WebService call garbled by nusoap:
$ Client-> soap_defencoding = 'utf-8'; $ client-> decode_utf8 = false; $ client-> xml_encoding = 'utf-8 ';
File Code cannot have any output , Otherwise, an error similar to the following will be reported during the call:
XML error parsing SOAP payload on Line X ( Row number ): Reserved XML name
Use Nusoap Implementation WebService, Do not enable PHP Of Soap Extension,The reason is that the soapclient class of nusoap conflicts with the soap class built in PhP5.
Solution
1. Modify PHP. ini and do not load the built-in soap extension of PhP5 (php_soap.dll in Windows ).
2. You have also changed the name of the nusoap soapclient class.
Identity Authentication
<? Phpheader ('content-type: text/XML; charset = UTF-8 '); require_once ('nusoap. PHP '); $ Params = array ('authenticationheader' => array ('content-type' => 'text/XML; charset = UTF-8 ', 'soapaction' => 'yourfunstion ',); $ client = new nusoap_client ('HTTP: // www.yourdomain.com/service.asmx? WSDL ', true, ''); $ client-> setheaders (' <TNS: authenticationheader xmlns: TNS =" http://tempuri.org/webservice "> <TNS: username> username </TNS: username> <TNS: Password> password </TNS: Password> </TNS: authenticationheader> '); $ err = $ client-> geterror (); if ($ ERR) {echo '<H2> constructor error </H2> <PRE> '. $ err. '</PRE>';} $ result = $ client-> call ('yourfunction', $ Params, '','', false, true); if ($ CLI Ent-> fault) {echo '<H2> fault </H2> <PRE>'; print_r ($ result); echo '</PRE> ';} else {$ err = $ client-> geterror (); if ($ ERR) {echo '<H2> error </H2> <PRE> '. $ err. '</PRE>';} else {echo '<H2> result </H2> <PRE>'; // print_r ($ result ); echo '</PRE>';} echo '<H2> request </H2> <PRE> '. htmlspecialchars ($ client-> request, ent_quotes ). '</PRE>'; echo '<H2> response </H2> <PRE> '. htmlspecialchars ($ client-> response, Ent_quotes). '</PRE>';?>