Web Services learning notes
Many organizations have adopted Apach and PHP as their web application environments. Using PHP in the Web Services mode may seem difficult. But in fact, you can easily use php5.0 to build the soap client and server.
What is Php?PHP: Hypertext Preprocessor (Hypertext Preprocessor, PHP) is a popular server-side scripting language used to create dynamic web content. The PHP interpreter providesSource codeOr compiled binary files. Which platforms include most Linux? Version, windows? , MacOS XAnd iseries ?.
Millions of web servers are running PHP, most of which use PHP 4. PHP 5 Launched in July 2004 is gradually being used. PHP 5 has improved the object model, and the underlying memory management has also been re-designed from the perspective of multithreading and performance. However, pay attention to a few changes that cannot be backward compatible. These are recorded in the PHP manual.
What is Web Service technology?Web services refer to self-contained and modular applications.ProgramThe client and service are loosely coupled in such applications. For details about Web Services, you only need to understand the main technologies in this article:
Soap (Simple Object Access Protocol) defines the messages transmitted between the client and the server. Messages are in XML format. Soap is independent of the Platform,Programming Language, Network, and transmission layer. This article will discuss soap over HTTP.
WSDL (Web Service Description Language) is an XML-based language used to describe Web services. The description includes the service location, format, operation, parameters, and data type.
UDDI (unified description, discovery, and integration) is a method that uses APIs and UDDI registry to store and retrieve web service information on the network.
This article includes some examples of soap messages and WSDL documents, but does not provide examples of UDDI.
The xmethods website is a useful web service tool, where you can find a list of publicly available Web Services implemented on various server platforms. You can use the examples in this article to easily access the services selected from xmethods
Soap and PHPThere are multiple products that allow the use of soap in PHP 4 scripts. The most common products are pear: soap and nusoap. Writing this articleArticleThese products still have problems in compatibility with PHP 5, and it is estimated that they will be upgraded soon.
The built-in soap extension is added in PHP 5, which is called ext/soap. It is provided as part of PHP, so you do not need to download, install, and manage separate packages. This is the first SOAP implementation written in PHP instead of C, so the author claims that it is much faster.
Because the new extension is one of the complete components of PHP, the relevant documentation is included in the function reference section of the PHP manual. The soap reference starts with an important Disclaimer:
Warning the extension is experimental ). The behavior of this extension, including the function name and other content of this extension, may be changed at any time in future PHP versions without notice. Use this extension at your own risk.
Warning seems a bit worrying, but in fact this extension seems to be well supported. And any newCodeThe extension also has defects, but the reported problems are usually fixed quickly. You can view the defect list on the PHP site. We estimate that in future PHP versions, this extension will switch from trial functionality to mainstream functionality.
Install PHP soap Extension
On Windows, you only need to add a line like extension = php_soap.dll after the line; Extension = php_zip.dll
Finally, ext/soap has its own configuration in PHP. ini. After the configuration is complete, ext/soap is shown as follows:
[Soap]
; Enables or disables WSDL caching feature.
Soap. wsdl_cache_enabled = 1
; Sets the directory name where soap extension will put cache files.
Soap. wsdl_cache_dir = "D:/web/PHP/soap_temp"
; (Time to live) sets the number of second while cached file will be used
; Instead of original one.
Soap. wsdl_cache_ttl = 86400
One example can illustrate everything. Let's take a look at one example.To illustrate how to use php5.0 to build Web services, we will give a simple example. The application in this example is composed of a server and a client of PHP Web Services. He will implement two functions: reversing the character order of a string, and finding the sum of the two numbers
Php soap serverIt is very easy to use php5.0 to create a SOAP server. Basically, you only need to write the functions that you want to expose to your web services, and then register them.
OK. In addition, two steps are required to establish the PHP SOAP server. First, you need to create an instance of the soap object in your PHP code, and then use the http post method to send the original data to soap for processing. It sounds simple.
First, let's take a look at listing 1.
Soapfunc. php
// Soapfunc. php
Function reverse ($ Str ){
$ Retval = "";
If (strlen ($ Str) <1 ){
Return new soapfault (''client'', ''', ''invalid string''); // It is possible to use PHP exception mechanic to throw soap fault
}
For ($ I = 1; $ I <= strlen ($ Str); $ I ++ ){
$ Retval. = $ STR [(strlen ($ Str)-$ I)];
}
Return $ retval;
}
Function add2numbers ($ num1, $ num2 ){
If (TRIM ($ num1 )! = Intval ($ num1 )){
Return new soapfault (''client'', ''', ''the first number is invalid '');
}
If (TRIM ($ num2 )! = Intval ($ num2 )){
Return new soapfault (''client'', ''', ''the second number is invalid '');
}
Return ($ num1 + $ num2 );
}
?>
The source file soapfunc. php is provided in ticket 1. This file contains two functions that we want to expose to Web services through the SOAP protocol: reverse and add2numbers. They are the core of our Web Services application. The reverse function contains a parameter that reverses the character order in the string and returns
Listing 2: soapserver. php
// Soapserver. php
Include_once (''soapfunc. php '');
$ Server = new soapserver (null, array (''uris ''=>"Http: // test-Uri/"); // The creationSoapserverObjects in WSDL or non-WSDL Mode
$ Server-> addfunction (''verse'); // exports one or more functions for remote clients
$ Server-> addfunction (''add2numbers ''); // exports one or more functions for remote clients
$ server-> handle (); // processes a SOAP request, CILS necessary functions, and sends a response back. the soap request. if this argument is omitted, the req