PHP SOAP Server
It is very easy to build a SOAP server with PHP and Nusoap. Basically, you just write out the functions you want to expose to your Web services, and then register them with Nusoap. OK, another two steps will be required to complete the creation of the PHP soap server. First you also create an instance of the Nusoap object in your PHP code, and then use the HTTP POST method to pass the raw data to nusoap for processing
The use of Nusoap is relatively simple, with the most commonly used classes being soap_server and SoapClient, where Soap_server is used to create the WebService service, and class soapclient is used to invoke WebService The definitions of these two classes are all in lib/nusoap.php, so we need to reference the file when we create or invoke the WebService interface program.
Nusoap is a WebService programming tool for creating or invoking webservice in a PHP environment. It is an open source software, developed by Nusphere Corporation (http://dietrich.ganx4.com/nusoap/), a series of PHP classes written entirely in PHP that send and receive SOAP messages over HTTP. One advantage of Nusoap is that there is no need for extended library support, which allows Nusoap to be used in all PHP environments and is not affected by server security settings.
1. First, go to http://sourceforge.net/projects/nusoap/ to download nusoap.zip.
2. Server : Establish nusoapservice.php file.
[Php]view plaincopy
-
- require_once ("lib/nusoap.php");
- $server = New soap_server ();
- //Avoid garbled characters
- $server ->soap_defencoding = ' UTF-8 ' ;
- $server ->decode_utf8 = false;
- $server ->xml_encoding = ' UTF-8 ' ;
- $server ->configurewsdl (' test '); //Open WSDL support
- /*
- registering programs that need to be accessed by clients
- type corresponding value: bool-> "Xsd:boolean" string-> "xsd:string"
- int-> "Xsd:int" float-> "Xsd:float "
- */
- $server ->register ( ' getteststr ', //Method name
- Array ("name" = " xsd:string" ), //parameter, default to "Xsd:string"
- Array ("return" = " xsd:string" )); //return value, default to "Xsd:string"
- //isset Detect if the variable is set
- $HTTP _raw_post_data = Isset ( $HTTP _raw_post_data )? $HTTP _raw_post_data : '' ;
- //service processing the data entered by the client
- $server ->service ( $HTTP _raw_post_data );
- /**
- * method for calling
- * @param $name
- */
- function getteststr ($name) {
- return "Hello, {$name}!" ;
- }
- ?>
3. Client: Establish the nusoapclient.php file.
[Php]view plaincopy
-
- require_once ("lib/nusoap.php");
- /*
- calling WebService via WSDL
- parameter 1 The address of the WSDL file (the WSDL after the question mark cannot be uppercase)
- Parameter 2 Specifies whether to use the 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 ' ;
- //parameter to array form pass
- $paras = Array (' name ' = ' Bruce Lee ' );
- //target method can omit subsequent arguments when no parameters are available
- $result = $client ->call ( ' getteststr ', $paras );
- //Check for error, get return value
- if (! $err = $client ->geterror ()) {
- Echo "return result:", $result;
- } Else {
- Echo "Call Error:", $err;
- }
- ?>
[Php]view plaincopy
-
- require_once ("lib/nusoap.php");
- /*
- calling WebService via WSDL
- parameter 1 The address of the WSDL file (the WSDL after the question mark cannot be uppercase)
- Parameter 2 Specifies whether to use the 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 ' ;
- //parameter to array form pass
- $paras = Array (' name ' = ' Bruce Lee ' );
- //target method can omit subsequent arguments when no parameters are available
- $client ->call ( ' getteststr ', $paras );
- $document = $client ->document;
- Echo $document;
- ?>
Note:Return Result: Hello, {Bruce Lee}!
Wsdl
WSDL is an XML language used to describe a Web service. It is a machine-readable format that provides all the information necessary to access a service to a Web service client. Nusoap specializes in providing a class for parsing WDSL files and extracting information from them. The SoapClient object uses a WSDL class to alleviate the difficulty of the developer invoking the service. With the help of the WSDL information to create the message, the programmer simply needs to know the name and parameters of the operation to invoke it.
Using WSDL with NUSOAP provides the following advantages:
All service meta-files, such as namespaces (namespaces), endpoint URLs, parameter names (parameter names), etc., can be obtained directly from the WSDL file, allowing the client to dynamically adapt to server-side changes. Because it is readily available from the server, this data no longer requires hard coding in user scripts.
It allows us to use the Soap_proxy class. This class derives from the SoapClient class and adds a method that corresponds to the operations detailed in the WDSL file. These methods can now be called directly by the user through it.
The SoapClient class contains a getproxy () square method that returns an object of a Soap_proxy class. The Soap_proxy class derives from the SoapClient class, adding a method that corresponds to the operation defined in the WSDL document, and allows the user to invoke a endpoint remote method. This applies only to cases where the SoapClient object is initialized with the Wdsl file. The advantage is that it is easy for users to use, and the disadvantage is that creating objects in performance--php is time-consuming-and does not serve utilitarian purposes (and this functionality serves no utilitarian purpose).
[Php]view plaincopy
-
- require_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 proxy class
- $proxy = $client ->getproxy ();
- //Call remote function
- $sq = $proxy ->getteststr (' Bruce Lee ');
- if (! $err = $proxy ->geterror ()) {
- Print_r ($sq);
- } Else {
- Print "ERROR: $err";
- }
- print ' REQUEST: '. <span>$p</span> <span>-&gt;request.</span> ' ' ;
- print ' RESPONSE: '. <span>Str_replace</span> <span>(</span><span>' &gt;&lt;</span>'<span>, </span> <span>' &gt;\n&lt;</span>'<span>, </span> <span>$p</span><span>-&gt;response).</span> ' ' ;
- ?>
Fourth step: Run Server Side file page: http://localhost/ Nusoapservice. PHP-Generated WSDL files
Click the method name. This allows us to provide a visual document to the service by using Nusoap by adding a few lines of code to the service. But that's not all we can do.
We add some WSDL calls in the service by using NUSOAP we can generate WSDL for the service and some other documentation. In contrast, there are few things we can do in the client, at least in our simple example. The client shown below is no different from the client without WSDL, the only difference being that parsing soapclent class is done by providing the URL to the WSDL, not the previous service endpoint.
Nusoap Call WebService the solution that appears garbled:
[Php]view plaincopy
- $client ->soap_defencoding = ' Utf-8 ' ;
- $client ->decode_utf8 = false;
- $client ->xml_encoding = ' Utf-8 ' ;
file code cannot have any output , Otherwise, the call will report an error similar to the following:
XML error parsing SOAP payload on line x ( row number ): Reserved XML Name
implement WebService with nusoap , do not open php SOAP extension, The reason is that Nusoap's soapclient class has conflicts with PHP5 's built-in soap classes.
Solution Solutions
1. Modify php.ini does not load PHP5 built-in SOAP extensions (Windows is Php_soap.dll).
2. There is also a renaming of the SoapClient class for Nusoap.
Identity verification
[Php]view plaincopy
-
- Header (' 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 ('
- "Http://tempuri.org/webservice" >
- username
- Password
-
- ');
- $err = $client ->geterror ();
- if ($err) {
- Echo '
Constructor Error
'$err'
';
- }
- $result = $client ->call (' yourfunction ', $params, ', ' , False, true);
- if ($client->fault) {
- Echo '
Fault
';
- Print_r ($result);
- Echo '';
- } Else {
- $err = $client ->geterror ();
- if ($err) {
- Echo '
Error
'$err'
';
- } Else {
- Echo '
Result
';
- //print_r ($result);
- Echo '';
- }
- }
- Echo '
Request
' . Htmlspecialchars ($client'
';
- Echo '
Response
' . Htmlspecialchars ($client'
';
- ?>
The above describes the webservice--nusoap, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.