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,
server side serverSoap.php
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>>
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.