PHP Implementation WebService instance, phpwebservice instance _php tutorial

Source: Internet
Author: User
Tags pear

PHP Implementation WebService instance, phpwebservice instance


In this paper, we explain how PHP implements WebService. Share to everyone for your reference. The implementation method is as follows:

First of all to understand what is webservice, and then do two very simple examples, WebService still can not open server side and client side.

The test environment here is: apache2.2.11 php5.2.10

Before you do this test, make sure that the SOAP extension is already open in your PHP configuration file, i.e.
Copy the Code code as follows: Extension=php_soap.dll;
OK Now let's experience webservice.

Server-side serversoap.php
Copy the code code as follows: $soap = new SoapServer 192.168.1.179/"));//this URI is your SERVER IP.
$soap->addfunction (' Minus_func ');//register the function
$soap->addfunction (soap_functions_all);
$soap->handle ();
Function Minus_func ($i, $j) {
$res = $i-$j;
return $res;
}
//client-clientsoap.php
try {
$client = new SoapClient (NULL,
Array (' location ' = 192.168.1.179/test/serversoap.php ", ' uri ' =" http://127.0.0.1/")
);
Echo $client->minus_func (100,99);
} catch (SoapFault $fault) {
echo "Error:", $fault->faultcode, ", String:", $fault->faultstring;
}
This is an example of a client invoking a server-side function, and we'll get a class.

Server-Side serversoap.php
Copy the Code code as follows: $classExample = Array ();
$soap = new SoapServer (Null,array (' uri ' = ' http://192.168.1.179/', ' classexample ' = = $classExample));
$soap->setclass (' Chesterclass ');
$soap->handle ();
Class Chesterclass {
Public $name = ' Chester ';
function GetName () {
return $this->name;
}
}
Client Side clientsoap.php
try {
$client = new SoapClient (NULL,
Array (' location ' = = ' http://192.168.1.179/test/serverSoap.php ', ' uri ' = ' http://127.0.0.1/')
);
echo $client->getname ();
} catch (SoapFault $fault) {
echo "Error:", $fault->faultcode, ", String:", $fault->faultstring;
}

I hope this article is helpful to everyone's PHP programming.


Who can give an example of PHP WebService

The basic idea of WEB service in order to communicate with heterogeneous systems is to provide a standard mechanism using remote invocation of XML-based HTTP without the need to establish a new protocol. Currently there are two protocol standards for Web service communication, one is XML-RPC and the other is soap. XML-RPC is relatively simple, the occurrence of time is relatively early, soap is more complex, mainly some need to be stable, robust, secure and complex interaction when used.

PHP integrates access to XML-RPC and soap two protocols, all focused on xmlrpc extensions. In addition, in PHP pear, whether PHP 4 or PHP 5, has been integrated by default XML-RPC extension, and the extension is independent of xmlrpc extension, can independently implement the XML-RPC protocol interaction, if there is no xmlrpc extension, it is recommended to use pear:: XML-RPC extension.

There is nothing more to say than that, the following example shows everything.

First, construct the WebService server:

On the server side, the function I define is get ("HelloWorld");//hello is the parameter passed in

/**
* Functions: Functions provided to RPC client calls
Parameters
* $method The function that the client needs to call
* $params the parameter array of the function that the client needs to call
* Return: Returns the result of the specified call
*/
function Rpc_server_func ($method, $params) {
$parameter = $params [0];//The name of the function passed in

$parameter 1 = $params [1];//parameters passed in
if ($parameter = = "Get")
{
$return = "This data by Get method". $parameter 1;
}
Else
{
$return = "Not specify method or params";
}
return $return;
}

Generate a XML-RPC server-side
$xmlrpc _server = Xmlrpc_server_create ();

Register a server-side call method Rpc_server, which is actually pointing to the Rpc_server_func function
Xmlrpc_server_register_method ($xmlrpc _server, "Rpc_server", "Rpc_server_func");

Accept the XML data that the client post comes from
$request = $HTTP _raw_post_data;

Get execution results after calling the client's XML request
$xmlrpc _response = Xmlrpc_server_call_method ($xmlrpc _server, $request, NULL);

Output the resulting XML after the function is processed
Header ("Content-type:text/xml");
echo $xmlrpc _response;

Destroying XML-RPC server-side resources
Xmlrpc_server_destroy ($xmlrpc _server);
?>

Use PHP to access the defined webse ... Remaining full text >>

How to invoke WebService instance reference in PHP _php tutorial

The code is really simple, when you create a SoapClient object, you can use to save the local WSDL file, you can use the remote address, the following array can take a lot of parameters, the specific parameters can be found in PHP soapclient help, here with the character set encoding, If you have Chinese in the parameters of the calling method, be sure to specify the character set encoding, or else an error will occur.

http://www.bkjia.com/PHPjc/907286.html www.bkjia.com true http://www.bkjia.com/PHPjc/907286.html techarticle PHP Implementation WebService instances, Phpwebservice Examples of this article describes the implementation of PHP WebService method. Share to everyone for your reference. The specific implementation method is as follows: First of all, we must be simple ...

  • 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.