Four simple architecture methods and instances implemented by webservice in php, phpwebservice_PHP tutorial

Source: Internet
Author: User
Tags php wsdl
The simple architecture and example of webservice implementation in four php types, phpwebservice. Simple architecture and examples of webservice implementation in four php types: phpwebservice 1: php soap all webservices include server and client ). You need to use the simple architecture methods and instances implemented by webservice in four php types, phpwebservice

I. PHP SOAP
All webservices include the server and client ).
To use php soap, you must first install and enable the extension. The specific code is as follows:
First, this is the server implementation:

The code is as follows:


<? Php
Class test
{
Function show ()
{
Return 'The data you request! ';
}
}
Function getUserInfo ($ name)
{
Return 'fbbin ';
}
// There is a parameter manual for instantiation. The wsdl is not used, so the first parameter is null. if the wsdl is used, the first parameter is the address of the wsdl file.
$ Server = new SoapServer (null, array ('URL' = 'http: // soap/', 'location' = 'http: // localhost/test/server. php '));
$ Server-> setClass ('test ');
// $ Server-> addFunction ('getuserinfo ');
$ Server-> handle ();
?>

Then the client

The code is as follows:


$ Soap = new SoapClient (null, array ('location' = 'http: // localhost/test/server. php ', 'uri' = 'http: // soap /'));
Echo $ soap-> show ();
// Get: 'The data you request! '
// Echo $ soap-> getUserInfo ('SS ');

That's simple. at that time, this was just a simple example. In fact, many communication mechanisms were implemented in this way!
//////////////////////////////////////// ////////////////////////////////////////

II. PHPRPC
First go to the official website (http://www.phprpc.org/zh_CN/) to download the latest version of phprpc, unzip the relevant files, we divide the files (server and client files) as follows:
Server files:

The code is as follows:


Dhparams
Dhparams. php
Phprpc_server.php
Bigint. php
Compat. php
Phprpc_date.php
Xxtea. php

Client files:

The code is as follows:


Phprpc_client.php
Bigint. php
Compat. php
Phprpc_date.php
Xxtea. php

We put the server file in the server folder, then put the client file in the client folder, and then create a new file in the server folder (server. php) as a service, and then create a file on the client (client. php) as the client, the code is as follows:
Server:

The code is as follows:


<? Php
Include_once "phprpc_server.php"; // load the phprpc file
$ Server = new PHPRPC_Server ();
$ Server-> add ('getuser ');
$ Server-> setDebugMode (true );
$ Server-> start ();
Function getUser ()
{
Return 'The data you request! ';
}

Client:
[Code]
<? Php
Include_once "phprpc_client.php ";
$ Client = new PHPRPC_Client ('http: // 127.0.0.1/phpservice/phprpcserver/server. php ');
$ Data = $ client-> getUser ();
Var_dump ($ data );
// Get the data you request!

As mentioned above, wsdl will be used to generate.
//////////////////////////////////////// ////////////////////////////////////////

3. open-source NUSOAP
First download the latest version of nusoap from the internet. The latest version is 0.9.5. decompress the package and you will get a lib folder. put the file to the server and client respectively, then, the server and the client establish the server respectively. php and client. php file as a communication file.
The server file is as follows:

The code is as follows:


<? Php
Ini_set ("soap. wsdl_cache_enabled", 0); // disable caching
Require_once ("lib/nusoap. php"); // load the nusoap file
$ Server = new soap_server;
$ Server-> configureWSDL ('nusoasp '); // set the service name and use the wsdl for communication. if the wsdl is not applicable, it will be simpler. There are many examples on the Internet.
$ Server-> register ('getuserinfo', array ('name' = "xsd: string", 'Email '= "xsd: string "), array ('Return '= "xsd: string "));
$ HTTP_RAW_POST_DATA = isset ($ HTTP_RAW_POST_DATA )? $ HTTP_RAW_POST_DATA :'';
$ Server-> service ($ HTTP_RAW_POST_DATA );
Function getUserInfo ($ name, $ email)
{
Return 'The data you request! ';
}

The client file is as follows:

The code is as follows:


Require_once ("lib/nusoap. php ");
$ Client = new soapclient ('http: // localhost/phpservice/nusoapserver/server. php? Wsdl ');
$ Pagram = array ('fbbin', 'fbbin @ foxmail.com ');
$ String = $ client-> call ('getuserinfo', $ pagram );
// Get the data you request!

//////////////////////////////////////// ///////////////////////////////////////

4. HessianPHP
Hessian actually I personally think that he is not a webservice and can only be said to be similar. Because it does not have the characteristics of webservice. It supports many languages. now we only need to study the php version of HessianPHP. download the latest version v2.0.3 and unzip it to get a src directory, this directory is a core folder that we need to use.
We rename the name HessianPHP and place it on the server and client respectively, and then create the server. php and client. php files respectively.
Server:

The code is as follows:


<? Php
Include_once 'hessianphp/HessianService. php'; // load the core file
Class TestService
{
Public function _ construct ()
{

}

Public function add ($ numa, $ numb)
{
Return $ numa + $ numb;
}

Public function check ()
{
Return 'fbbiin @ gmail.com ';
}
}
$ Test = new TestService ();
$ Hessian = new HessianService ($ test, array ('displayinfo' => true ));
$ Hessian-> handle (); // note that this is not the $ hessian-> service () on the internet. the version may be different. change it! I also read the source code to know!
?>

Client:

The code is as follows:


<? Php
Include_once 'hessianphp/HessianClient. php ';
$ Url = "http: // localhost/phpservice/hessianserver/server. php ";
$ Options = new HessianOptions ();
$ Client = new HessianClient ($ url, $ options );
$ Num = $ client-> add (3, 5 );
Echo $ num; // Get: 8;
Echo $ client-> check (); // Get: fbbiin@gmail.com;

The preceding four methods are commonly used in web development. Nusoap is the most used. I personally think phprpc is actually quite good. it is basically similar to nusoap in terms of performance, but phprpc is commercially charged. Another hessianPHP seems to use java to transmit data streams in binary mode. For more details, go to Baidu and Google.
The following describes how to generate a wsdl file.
The most widely used and relatively secure communication on webservice is the use of wsdl. such files can be written by yourself, but not necessarily cannot be written, therefore, we need to use zend studio to generate the wsdl file.
Next we will generate the WSDL File, File-> New-> Other-> Web Service-> WSDL, so that we can create a New WSDL File ,.


Then we will modify the WSDL file. zeng studio provides us with visual operations. of course, if you are a good user, you can change the file code. In fact, there are just a few things, it will not be too difficult to understand.


After this step, the WSDL file is basically available, but you need to pay attention to the following two issues:
To do this, the test may fail, and the binding may not be performed. this task sometimes needs to be completed manually, right-click the binding and select Generate Binding Content (that is, the small box in the middle of the two boxes.
The second thing to note is the php WSDL cache. during the test, the WSDL cache is usually disabled. Otherwise, you may use the original WSDL file instead of the updated one. There are two ways to disable caching. The first method is to directly access php. set soap in ini. wsdl_cache_enabled = 0; the second is to add a statement in the PHP file, ini_set ("soap. wsdl_cache_enabled "," 0 ");
By doing this, you can test and call your server program with confidence.
Finished. OK!

Protocol 1: php soap all webservices include server and client ). To use...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.