Simple implementation of four WebServices in php

Source: Internet
Author: User
Tags php wsdl
I. All the WebServices of PHP SOAP include server and client ). To use php soap, you must first install and enable the extension. Next, let's take a look at the specific code. First, this is the server implementation: How does PHPCode copy the content to the clipboard? Phpclasstest {functionshow () {returntheda

I. All the WebServices of PHP SOAP include server and client ). To use php soap, you must first install and enable the extension. Next, let's take a look at the specific code. First, this is the server implementation: How does PHP Code copy content to the clipboard? Php class test {function show () {return 'theda

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:

Copy content from PHP Code to clipboard

  1. Class test
  2. {
  3. Function show ()
  4. {
  5. Return 'the data you request! ';
  6. }
  7. }
  8. Function getUserInfo ($ name)
  9. {
  10. Return 'fbbin ';
  11. }
  12. // 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.
  13. $ Server = new SoapServer (null, array ('url' = 'HTTP: // soap/', 'location' = 'HTTP: // localhost/test/server. php '));
  14. $ Server-> setClass ('test ');
  15. // $ Server-> addFunction ('getuserinfo ');
  16. $ Server-> handle ();
  17. ?>

Then the client

Copy content from PHP Code to clipboard

  1. $ Soap = new SoapClient (null, array ('location' = 'HTTP: // localhost/test/server. php ', 'uri' = 'HTTP: // soap /'));
  2. Echo $ soap-> show ();
  3. // Get: 'The data you request! '
  4. // 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:

Copy content from PHP Code to clipboard

  1. Dhparams
  2. Dhparams. php
  3. Phprpc_server.php
  4. Bigint. php
  5. Compat. php
  6. Phprpc_date.php
  7. Xxtea. php

Client files:

Copy content from PHP Code to clipboard

  1. Phprpc_client.php
  2. Bigint. php
  3. Compat. php
  4. Phprpc_date.php
  5. 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:

Copy content from PHP Code to clipboard

  1. Include_once "phprpc_server.php"; // load the phprpc File
  2. $ Server = new PHPRPC_Server ();
  3. $ Server-> add ('getuser ');
  4. $ Server-> setDebugMode (true );
  5. $ Server-> start ();
  6. Function getUser ()
  7. {
  8. Return 'the data you request! ';
  9. }
  10. Client:
  11. [Code]
  12. Include_once "phprpc_client.php ";
  13. $ Client = new PHPRPC_Client ('HTTP: // 127.0.0.1/phpservice/phprpcserver/server. php ');
  14. $ Data = $ client-> getUser ();
  15. Var_dump ($ data );
  16. // 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:

Copy content from PHP Code to clipboard

  1. Ini_set ("soap. wsdl_cache_enabled", 0); // disable caching
  2. Require_once ("lib/nusoap. php"); // load the nusoap File
  3. $ Server = new soap_server;
  4. $ 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.
  5. $ Server-> register ('getuserinfo', array ('name' = "xsd: string", 'email '= "xsd: string "), array ('Return '= "xsd: string "));
  6. $ HTTP_RAW_POST_DATA = isset ($ HTTP_RAW_POST_DATA )? $ HTTP_RAW_POST_DATA :'';
  7. $ Server-> service ($ HTTP_RAW_POST_DATA );
  8. Function getUserInfo ($ name, $ email)
  9. {
  10. Return 'the data you request! ';
  11. }


The client file is as follows:

Copy content from PHP Code to clipboard

  1. Require_once ("lib/nusoap. php ");
  2. $ Client = new soapclient ('HTTP: // localhost/phpservice/nusoapserver/server. php? Wsdl ');
  3. $ Pagram = array ('fbbin', 'fbbin @ foxmail.com ');
  4. $ String = $ client-> call ('getuserinfo', $ pagram );
  5. // 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:

Copy content from PHP Code to clipboard

  1. Include_once 'hessianphp/HessianService. php'; // load the Core File
  2. Class TestService
  3. {
  4. Public function _ construct ()
  5. {
  6. }
  7. Public function add ($ numa, $ numb)
  8. {
  9. Return $ numa + $ numb;
  10. }
  11. Public function check ()
  12. {
  13. Return 'fbbiin @ gmail.com ';
  14. }
  15. }
  16. $ Test = new TestService ();
  17. $ Hessian = new HessianService ($ test, array ('displayinfo' => true ));
  18. $ 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!
  19. ?>


Client:

Copy content from PHP Code to clipboard

  1. Include_once 'hessianphp/HessianClient. php ';
  2. $ Url = "http: // localhost/phpservice/hessianserver/server. php ";
  3. $ Options = new HessianOptions ();
  4. $ Client = new HessianClient ($ url, $ options );
  5. $ Num = $ client-> add (3, 5 );
  6. Echo $ num; // get: 8;
  7. 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!

Reference:
1: http://winphfar.iteye.com/blog/559.pdf;
2: http://www.iteye.com/blogs/tag/phprpc
3: http://yeyuan.iteye.com/blog/1261491
4: http://www.cnblogs.com/helloxyz/archive/2011/11/5.html
5: http://www.phpx.com/happy/thread-122030-1-1.html

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.