PHP & WebService 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] /**
* 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]
/**
* 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]
/**
* 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?