Unlike other ajax frameworks, PHPRPC follows the principle of "only do one thing and do it well. It is only responsible for data transmission, and will be the best transmission! It makes it easy for you to do things that were previously impossible or difficult to do with the traditional ajax method!
I believe that one of the main reasons why many people choose PHPRPC is that it makes it easier for you to develop ajax applications! Although the power of PHPRPC is not limited to this, I have to admit that this is indeed a highlight of PHPRPC!
So, let's take a look at how to use PHPRPC 3.0 to compile ajax applications.
The following is a simple example (or even a boring example) to illustrate how to use PHPRPC 3.0 to write ajax applications. This example shows how the MVC pattern mentioned above works. Here we will take the PHP server as an example. The first example is simple. The client inputs a string and the server calculates its SHA1 value.Copy codeThe Code is as follows: <? Php
Require_once ("../php/phprpc_server.php ");
$ Server = new PHPRPC_Server ();
$ Server-> add ("sha1 ");
$ Server-> start ();
?>
With such a simple 4-line code, PHP's built-in sha1 function is released. Now, the client can directly use this function.Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> calculate SHA1 </title>
<Script type = "text/javascript" src = "../js/compressed/phprpc_client.js"> </script>
<Script type = "text/javascript">
Var rpc = new PHPRPC_Client ('sha1. php', ['sha1']);
Function $ (id ){
Return document. getElementById (id );
}
Function showResult (result ){
$ ('Sha1'). value = result;
}
Function sha1 (){
Var input = $ ('sha1'). value;
$ ('Sha1'). value = "Loading ...";
Rpc. sha1 (input, showResult );
}
</Script>
</Head>
<Body>
<Input type = "text" id = "sha1"/>
<Input type = "button" value = "Calculate SHA1" onclick = "sha1 ()"/>
</Body>
</Html>
This example is very simple, and there are only two statements related to PHPRPC. One is:Copy codeThe Code is as follows: var rpc = new PHPRPC_Client ('sha1. php', ['sha1']);
This statement is used to create a PHPRPC_Client object. The first parameter is the server address. The relative path or absolute path can be used here.
And this path can be cross-origin! Therefore, you can use cross-Origin applications!
The second parameter is the list of server-side function names that you need to use. Therefore, although we only have one function, we also need to write it as an array. In this way, we have a client object that can call the server method.
How can we call it? Let's look at the second statement about PHPRPC:Copy codeThe Code is as follows: rpc. sha1 (input, showResult );
This statement is quite simple. You will find that we use the function name published on the server side as a method for calling the rpc object. Its first parameter is the value of the sha1 function. The second parameter is a function, which is a callback function. That is to say, after the server method is executed, the function is automatically called to process the result. It is defined previously. You will find that it has a parameter result, which is the return value of sha1 in our remote process. It is passed through this parameter of the callback function.