Http://www.phpchina.com/html/21/t-12821.html
Not much nonsense, WebService is hot, PHP is hot, and it is a matter of course to develop WebService with PHP. What? Do not understand WebService? It's okay. Baidu will know it at once. Ah? Why not? It doesn't matter if you don't understand it. Let's look at what the young master wrote :)
The young master is a radical and prefers to talk about the Code directly. However, before getting the poor readers confused, let's give a rough look at the purpose of this Article. This article does not describe the concept of WebService, nor describe the complicated protocol content. This article only shows you what WebService and PHP can do, how to do it, and what to pay attention.
If you have heard of Baidu, or you have checked it before, you probably know the usefulness of WebService. Through WebService, we can call programs deployed elsewhere, instead of the platform on which the called programs are written. Maybe you have heard of googleapi, yahooapi or something. Although you haven't investigated it one by one, you know that Google is implemented according to the standard web service, and Google uses Java, we will use PHP here.
Start: Hello word!
Let's first look at the Code:
[PHP] <? PHP
/**
* Class that implements business logic. This class is a common class.
*
*/
Class basic {
/**
* Returns a string: Hello world!
*
* @ Return string
*/
Public function returnString (){
Return "Hello World! ";
}
}
?> [/Php]
So is simple. A class returns a string. Suppose this program is on server A. What if the young master wants to call this program on server B? First, we need to turn this class into a Web service, which requires the php soap extension. For how to install this extension, see the PHP manual. After installation, see the following code.
[Php]
<? Php
/**
* The Web Service Server, including class files.
*/
Require_once ("Basic. php ");
/**
* Create a Server Object
*/
$ ArrOptions = array ('url' => 'HTTP: // example.com/'); // you can specify a namespace.
$ ObjSoapServer = new SoapServer (null, $ arrOptions );
/**
* Register all methods of the Basic class
*/
$ ObjSoapServer-> setClass ("Basic ");
/**
* Request processing
*/
$ ObjSoapServer-> handle ();
?> [/Php]
Okay, a Web service has been built. In the above example, we created a SoapServer object, and then used the setClass function to set the Basic class we just compiled. Finally, handle (), let's ignore the namespace. Now, if you use a browser to access this file, a warning is displayed. This is normal because the file is not accessed by a browser, but accessed by another program. When a client program accesses our WebService and runs to handle (), IT processes the input of the client and outputs the result to each user. Note that you should not output anything before or after handle (), otherwise the client will not be able to handle it.
Now, we need to write a client to access this WebService.
[Php]
<? Php
/**
* Client: first, create a Client object.
*/
$ ArrOptions = array ('url' => 'HTTP: // example.com/', // set the namespace
'Location' => 'HTTP: // URL/to/our/webservice', // set the server address
'Track' => true );
$ Objsoapclient = new soapclient (null, $ arroptions );
/**
* Remote Call
*/
Try {
$ Strreturn = $ objsoapclient-> returnstring ();
} Catch (exception $ e ){
}
/**
* Print the result
*/
Echo $ strreturn;
?>
[/PHP]
In the above program, we first create a SoapClient object, then call the returnString method just like using the Basic object on the server side, and print the output result. No matter which server you put this PHP on, as long as it can be connected to the server network, we can get the expected result: Hello World !.
Both SoapServer and SoapClient receive two parameters. The second parameter is Option, which supports several options. Here we use:
Uri: namespace. The client and server must use the same namespace.
Location: used by the client to specify the access address of the server program, that is, the address of the second code in this example.
Trace: used by the client. If it is true, the content of the communication between the server and the client can be obtained for debugging.
You can try to extend the first code and enter more functions to see what the returned integer, floating point, array, and object look like. You can also try to receive parameters from the client, after processing the information, you can use the SoapClient function to track the communication content between the client and the server. (For more information about these functions, see the PHP documentation.
If you try what the young master says, you will find that basically what data type is returned by the server and what data type can be received by the client, but when the server returns an object, the client receives an stdClass object, and its attribute visibility is not defined by the server, but all public.
Experienced readers may ask, what if the client and server encoding is different? In Option, there is an encoding Option that can be used on the client and server to specify the encoding of text. For example, if your server is gbk encoded and the client is utf8 encoded, fill in gbk and utf8 on the server and client respectively. Note: This option does not change the encoding of the communication content between the client and the server (it is always utf8), but only performs encoding conversion after the two ends receive the communication content.
Well, it should be difficult to finish the first example. However, this is only the most basic thing. We know that PHP is a weak language. For strong languages (C, C ++, Java ), how do they identify the data types returned by PHP?