Home  >  Article  >  Backend Development  >  PHP implements webservice instance, phpwebservice instance_PHP tutorial

PHP implements webservice instance, phpwebservice instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:57804browse

php implements webservice instance, phpwebservice instance

The example in this article describes how to implement webservice in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

First of all, everyone needs to briefly understand what webservice is. Next, we will give two very simple examples. Webservice still cannot escape the server side and client side.

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

Before doing this test, make sure that the soap extension has been turned on in your php configuration file, that is,

Copy code The code is as follows:
extension=php_soap.dll;

OK now let’s experience webservice

server side serverSoap.php

Copy code The code is as follows:
$soap = new SoapServer(null,array('uri'=>"http://192.168.1.179 /"));//This uri is your SERVER ip.
$soap->addFunction('minus_func'); $soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();
function minus_func($i, $j){
$res = $i - $j;
Return $res;
}
//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->minus_func(100,99);
} catch (SoapFault $fault){
echo "Error: ",$fault->faultcode,", string: ",$fault->faultstring;
}
This is an example of the client calling a server-side function. Let’s create a class.
server side serverSoap.php


Copy code The code is 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 will be helpful to everyone’s PHP programming design.

Who can give an example of php webservice

Web Service was created for the communication of heterogeneous systems. Its basic idea is to use XML-based HTTP remote calls to provide a standard mechanism, eliminating 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 and appeared earlier, while SOAP is more complex and is mainly used when stability, robustness, security and complex interactions are required.

PHP integrates access to two protocols, XML-RPC and SOAP, both of which are concentrated in the xmlrpc extension. In addition, in PHP's PEAR, whether it is PHP 4 or PHP 5, the XML-RPC extension has been integrated by default, and this extension has nothing to do with the xmlrpc extension and can independently implement XML-RPC protocol interaction. If there is no xmlrpc extension, it is recommended Use the PEAR::XML-RPC extension.

No matter how much you say, it is all false. The following example illustrates everything.

First construct the server side of the webservice:

On the server side, the function I defined is get( "helloworld" );//hello is the incoming parameter

< ;?php
/**
* Function: Function provided to the RPC client to call
* Parameters:
* $method Function that the client needs to call
* $params Needed by the client Parameter array of the called function
* Return: Return the specified call result
*/
function rpc_server_func($method, $params) {
$parameter = $params[0];//pass in Function name

$parameter1 = $params[1];//Incoming parameters
if ($parameter == "get")
{
$return = "This data by get method".$parameter1 ;
}
else
{
$return = "Not specify method or params";
}
return $return;
}

//Generate an XML-RPC server
$xmlrpc_server = xmlrpc_server_create();

//Register a method rpc_server called by the server, which actually points to the rpc_server_func function
xmlrpc_server_register_method($xmlrpc_server, "rpc_server", "rpc_server_func");

//Accept the XML data POST from the client
$request = $HTTP_RAW_POST_DATA;

//Execute the call Get the execution result after the client's XML request
$xmlrpc_response = xmlrpc_server_call_method($xmlrpc_server, $request, null);

//Output the result XML after function processing
header("Content- Type: text/xml");
echo $xmlrpc_response;

//Destroy XML-RPC server-side resources
xmlrpc_server_destroy($xmlrpc_server);
?>

Use php to access the defined webse...the rest of the text>>

Instance reference of how to call webservice in PHP_PHP tutorial

The code is indeed very simple. When creating a SoapClient object, you can use a WSDL file saved locally or a remote address. The following array can contain many parameters. For specific parameters, you can check the SoapClient help of PHP. What is included here is the character set encoding. If there are Chinese characters in the parameters of the calling method, the character set encoding must be specified, otherwise an error will occur.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907286.htmlTechArticlephp implements webservice examples, phpwebservice examples This article describes how php implements webservices. Share it with everyone for your reference. The specific implementation method is as follows: First of all, everyone must be simple...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn