This example introduces four methods for implementing the Webservice architecture using PHP.

Source: Internet
Author: User
Tags php wsdl
This example introduces four methods for implementing the Webservice architecture using PHP. I know the following types of webservices: PHP SOAP, open-source NUSOAP, and commercial PHPRPC, as well as the use of HessianPHP for binary data stream transmission, we will briefly introduce the use of these webservices in php. although there are many materials on the Internet, this is my personal experience, of course, I am also looking for materials from the internet. here I will take a simple note.

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:

PHP code

 SetClass ('test'); // $ server-> addFunction ('getuserinfo'); $ server-> handle ();?>

Then the client

PHP code

$ Soap = new SoapClient (null, array ('location' = 'http: // localhost/test/server. php', 'uri '= 'http: // soap/'); echo $ soap-> show (); // you can get the following information: '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:

PHP code

dhparams     dhparams.php     phprpc_server.php     bigint.php     compat.php     phprpc_date.php     xxtea.php  

Client files:

PHP code

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:

PHP code

 Add ('getuser'); $ server-> setDebugMode (true); $ server-> start (); function getUser () {return 'The data you request! ';}

Client:

PHP code

 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:

PHP code

 ConfigureWSDL ('nusoasp '); // set the service name and use the wsdl for communication. it will be simpler if the wsdl is not applicable, 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:

PHP code

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:

PHP code

 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:

PHP code

 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!

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.