Simple architecture method and instance _php example of webservice implementation in four kinds of PHP

Source: Internet
Author: User
Tags soap wsdl

One: The soap of PHP itself
All webservice include the service side (server) and clients (client).
The first thing to do with PHP itself is to install the extension and enable it. Look at the specific code below
First, this is the service-side implementation:

Copy Code code as follows:

<?php
Class Test
{
Function Show ()
{
Return ' The data for you request! ';
}
}
function GetUserInfo ($name)
{
Return ' Fbbin ';
}
Instantiated parameters manual above, this is not using WSDL, so the first parameter is NULL, if there is a WSDL, then the first parameter is the address of this WSDL file.
$server = new SoapServer (null, array (' uri ' = ' http://soap/', ' location ' = ' http://localhost/test/server.php '));
$server->setclass (' Test ');
$server->addfunction (' GetUserInfo ');
$server->handle ();
?>

Then the client

Copy Code code 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 (' sss ');

So simple, at that time this is just a very simple example, in fact, a lot of communication mechanisms are so to achieve!
////////////////////////////////////////////////////////////////////////////////

Two: Phprpc
First to the official website (http://www.phprpc.org/zh_CN/) above to download the latest version of the PHPRPC, after decompression will have related documents, we divided the file (server and client files) as follows:
Server-side files:

Copy Code code as follows:

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

Client file:

Copy Code code as follows:

phprpc_client.php
bigint.php
compat.php
phprpc_date.php
xxtea.php

We put the server-side files in the server-side folder, then put the client files in the client folder, then the server-side folder in the new file (server.php) as a service, and then the client to create a new file (client.php) as the client, the respective code is as follows:
Server End:

Copy Code code as follows:

<?php
Include_once "phprpc_server.php"; Load Phprpc File
$server = new Phprpc_server ();
$server->add (' GetUser ');
$server->setdebugmode (TRUE);
$server->start ();
function GetUser ()
{
Return ' The data for you request! ';
}

Client side:
[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!

This refers to WSDL and then to how to build it.
////////////////////////////////////////////////////////////////////////////////

Third: Open source of the Nusoap
First go online to download the latest version of Nusoap, Now the latest version is 0.9.5, decompression will get a Lib folder, put this file to the server and the client each, and then the server and the client to establish server.php and client.php files, as a communication file.
The server-side files are as follows:

Copy Code code as follows:

<?php
Ini_set ("soap.wsdl_cache_enabled", 0);//Close cache
Require_once ("lib/nusoap.php"); Load Nusoap File
$server = new Soap_server;
$server->configurewsdl (' nusoasp ');/set the name of the service, use the WSDL to communicate, if not the WSDL will be simpler, there are many examples on the web
$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 for you request! ';
}

The client files are as follows:

Copy Code code 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!

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

Four: hessianphp
Hessian in fact, I personally think he is not a webservice, can only say is similar. Because it does not have the characteristics of the webservice. It supports a lot of languages we now only need to study the PHP version of the hessianphp on the line, download the latest version is v2.0.3, after decompression will get a src directory, this directory allows us to use a core folder.
We rename the names to hessianphp and then separate them into the server and client side, and then build the server.php and client.php files separately.
Server End:

Copy Code code as follows:

<?php
Include_once ' hessianphp/hessianservice.php '/load 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 an online $hessian->service (), may be a different version, changed it! I also saw the source code just know!
?>

Client side:

Copy Code code 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 above four methods are commonly used in the Web development process to webservice communication methods. Use the most is nusoap, personal feeling phprpc actually also good, this in performance and Nusoap basically similar, but, PHPRPC in commercial is charged. There is also a hessianphp seems to be in Java, using the binary mode of transmission data flow, in fact, is also the same. More detailed information to find Baidu and Google bar.
Next, generate a WSDL file.
The most and relatively secure thing we use to communicate above webservice is using WSDL, which can be written on its own, but it doesn't seem to be a certain Daniel, so we need a tool Zend studio to generate the WSDL file.
Here we will generate the WSDL file, File->new->other->web service->wsdl, so that we can create a new WSDL file, as shown in the figure.


Then we will modify the WSDL file, Zeng Studio provides us with visual operations, of course, if you are cattle, you can certainly change the file code, in fact, a few things, understand the words will not be too difficult.


After this step, the WSDL file is basically available, but two issues need to be noted:
To do this, it is possible to test the failure, may be because there is no Binding, this thing sometimes need to be done manually, the Binding on the right to select Generate Binding Content (that is, the box in the middle of two big box) on the line.
The second thing to note is PHP's WSDL cache, which, when tested, typically shuts down the WSDL cache, otherwise you may have used the original WSDL file instead of the updated one. There are two ways to turn off caching, the first of which is to set soap.wsdl_cache_enabled = 0 directly into php.ini, and the second is to add a statement to the PHP file, Ini_set ("soap.wsdl_cache_enabled", "0" );
By doing this, you can safely test and invoke your server program.
I'm done, 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.