Developing Web Services Using PHP
Pages: 1, 2, 3, 4
Create a PHP script, xmlrpc-webservice.php, in the C:/Apache2/htdocs directory. In the PHP script, define a function, hello_func. Any function that is invoked by a client is required to take three parameters: the first parameter is the name of the XML-RPC method invoked. The second parameter is an array containing the parameters sent by the client. The third parameter is the application data sent in the user_data parameter of the xmlrpc_server_call_method() function. In the hello_func function, retrieve the first parameter, which is a name sent by the client, and output a Hello message.
function hello_func($method_name, $params, $app_data)
{
$name = $params[0];
return "Hello $name.";
}
Create an XML-RPC server using the xmlrpc_server_create() method.
$xmlrpc_server=xmlrpc_server_create();
If a server does not get created the xmlrpc_server_create method returns FALSE. Register the hello_func function with the server using the xmlrpc_server_register_method method. The first argument to the xmlrpc_server_register_method method is the XML-RPC server resource. The second argument is name of the method that is provided by the web service, which is the <methodName> element value in a XML-RPC request. The third argument is the PHP function to be registered with the server.
$registered=xmlrpc_server_register_method ($xmlrpc_server, "hello", "hello_func" );
If the PHP function gets registered, the xmlrpc_server_register_method returns TRUE.
Creating an XML-RPC Client
Next, I shall create a XML-RPC client to send a request to the XML-RPC server. First, specify the XML string to be sent in the request.
$request_xml = <<< END
<?xml version="1.0"?>
<methodCall>
<methodName>hello</methodName>
<params>
<param>
<value>
<string>Deepak</string>
</value>
</param>
</params>
<methodCall>
END;
To escape XML in PHP, <<<END ….END; is used. An XML document demarker other END may also be used. The methodCall element specifies the web service method that is to be invoked. Invoke the web service method using the xmlrpc_server_call_method function. The first argument to the xmlrpc_server_call_method function is the server resource. The second argument is the string containing the XML-RPC request. The third argument is the application data that is sent to the third parameter of the method handler function.
$response=xmlrpc_server_call_method( $xmlrpc_server, $request_xml, '', array(output_type => "xml"));
Output the XML-RPC response from the server.
print $response;
The PHP script, xmlrpc-webservice.php, is listed below.
<?php
function hello_func($method_name, $params, $app_data)
{
$name = $params[0];
return "Hello $name.";
}
$xmlrpc_server=xmlrpc_server_create();
$registered=xmlrpc_server_register_method
($xmlrpc_server, "hello", "hello_func" );
$request_xml = <<< END
<?xml version="1.0"?>
<methodCall>
<methodName>hello</methodName>
<params>
<param>
<value>
<string>Deepak</string>
</value>
</param>
</params>
</methodCall>
END;
$response=xmlrpc_server_call_method( $xmlrpc_server,
$request_xml, '', array(output_type => "xml"));
print $response;
?>
Copy the xmlrpc-webservice.php script to the C:/Apache2/htdocs directory. Invoke the PHP script with the URL http://localhost/xmlrpc-webservice.php. The response from the server gets output to the browser as shown in Figure 2.

Figure 2. Response from XML-RPC web service
To demonstrate an error in the request, returned as a <fault> element in the response XML, make the request XML not valid XML. For example replace </methodCall> with <methodCall> in the $request_xml. Invoke the xmlrpc-webservice.php. The response from the server is returned as a <fault> element that consists of a struct element value, which consists of faultCode and faultString members, as shown in Figure 3.

Figure 3. Response as fault element
References
For more information, see W3C's WSDL page and SOAP Primer.
Deepak Vohra is a NuBean consultant and a web developer.
Return to ONLamp.com.
-
about targetNameSpace attribute of defination element
2010-03-26 02:18:23 shreehart [View]
-
saop example issue
2010-01-06 03:43:15 johnyp@ [View]
-
Error When Im trying to Execute webservices in php
2009-12-10 02:55:24 M.ESWAR [View]
-
Fatal error: Uncaught SoapFault exception: [Client] DTD are not supported by SOAP in...
2009-11-30 18:31:52 obsidiana [View]
-
SoapFault exception: [Client] looks like we got no XML document
2007-09-21 21:53:53 jwan_nguyen [View]
-
Error when running the page
2007-08-25 09:36:35 gavin-ganesh [View]
-
Using a class instead of explicit function mapping
2007-08-02 14:28:50 Ed_D [View]
-
PHP Web Services
2007-07-26 22:38:56 MattMan [View]