Four simple architectural methods and examples of WebService implementations in PHP, phpwebservice_php tutorial

Source: Internet
Author: User
Tags php wsdl

Four simple architecture methods and examples webservice implemented in PHP, Phpwebservice


One: The soap of PHP itself
All webservice include service-side (server) and client.
To use the soap in PHP itself, first install and enable the extension. Here's a look at the specific code
First, this is the service-side implementation:

Copy the Code code as follows:
<?php
Class Test
{
Function Show ()
{
Return ' The data you request! ';
}
}
function GetUserInfo ($name)
{
Return ' Fbbin ';
}
Instantiated parameter manual above, this is not using WSDL, so the first parameter is NULL, if you use 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 the 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/) to download the latest version of the PHPRPC, after the decompression will have the relevant files, we divide the file (server and client files) as follows:
Service-Side files:

Copy the Code code as follows:
Dhparams
dhparams.php
phprpc_server.php
bigint.php
compat.php
phprpc_date.php
xxtea.php

Client files:

Copy the Code code as follows:
phprpc_client.php
bigint.php
compat.php
phprpc_date.php
xxtea.php

We put the server file in the server folder, and then put the client file in the client folder, then the server 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 side:

Copy the Code code 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 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 how the WSDL is generated.
////////////////////////////////////////////////////////////////////////////////

Three: Open source of Nusoap
First go online to download the latest version of Nusoap, Now the latest version is 0.9.5, after the decompression will be a Lib folder, the file is placed on 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 files are as follows:

Copy the Code code as follows:
<?php
Ini_set ("soap.wsdl_cache_enabled", 0);//Close cache
Require_once ("lib/nusoap.php"); Load the Nusoap file
$server = new Soap_server;
$server->configurewsdl (' nusoasp ');//Set the name of the service, use the WSDL to communicate, if the WSDL is not applicable will be more simple, there are many examples online
$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 files are as follows:

Copy the 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 actually I personally think he is not a webservice, can only say is similar. Because it does not have those characteristics of webservice. It supports a lot of languages we now only need to study the PHP version of the hessianphp, download the latest version is v2.0.3, decompression will get a src directory, this directory makes us need to use a core folder.
We renamed the names to hessianphp and then respectively to the server and client side, and then set up the server.php and client.php files respectively.
Server side:

Copy the 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 this is not online $hessian->service (), maybe the version is not the same, change it! I also read the source to know!
?>

Client side:

Copy the 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;//Received: 8;
echo $client->check ();//get: fbbiin@gmail.com;

The above four methods are commonly used in the Web development process WebService communication mode. The most used is nusoap, personal feeling phprpc is actually good, the performance above and Nusoap basically almost, but, PHPRPC in the commercial is charged. There is a hessianphp seems to be used in Java, the use of binary mode of transmission of data flow, in fact, there are also a few. More detailed information to find Baidu and Google it.
Next, generate the WSDL file.
The most and relatively safe way to communicate with WebService is to use WSDL, which can be written on its own, but it is not certain that Daniel seems to be able to write, so we need a tool Zend studio to generate the WSDL file.
Now we're going to build the WSDL file, File->new->other->web service->wsdl, so we can create a new WSDL file.


Then we have to modify the WSDL file, Zeng Studio for us to provide a visual operation, of course, if you cow, you can certainly change the file code, in fact, a few things, understand the words will not be too difficult.


Once this is done, the WSDL file is basically available, but two more questions need to be noted:
To do this, it is possible to test the failure, it may be because there is no binding, this thing sometimes need to be done manually, on the binding right-click on the Generate binding Content (that is, two large boxes in the middle of the small box) on the line.
The second thing to note is the PHP WSDL cache, which is typically closed for the WSDL cache when testing, otherwise you might be using the original WSDL file instead of the updated one. There are two ways to turn off caching, the first 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!.

http://www.bkjia.com/PHPjc/953151.html www.bkjia.com true http://www.bkjia.com/PHPjc/953151.html techarticle There are four simple architectural methods and examples of WebService implementations in PHP, Phpwebservice: All of the soap WebService in PHP itself includes the server and client. To use ...

  • Related Article

    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.