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.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 8 of 8.
-
SoapFault exception: [Client] looks like we got no XML document
2007-09-21 21:53:53 jwan_nguyen [Reply | View]
-
SoapFault exception: [Client] looks like we got no XML document
2007-09-22 08:03:55 Deepak Vohra | [Reply | View]
Can't find HTTP_RAW_POST_DATA is a bug in PHP 5.2.2. What is the PHP version?
http://bugs.php.net/bug.php?id=41293&edit=1
-
Error when running the page
2007-08-25 09:36:35 gavin-ganesh [Reply | View]
Im using the script to create a webservice under WAMP environment. But im getting this error
Class 'SoapClient' not found
am i missing any includes...
pls. help
-ganesh -
Error when running the page
2007-08-25 10:59:21 Deepak Vohra | [Reply | View]
Is the PHP SOAP extension enabled?
-
Using a class instead of explicit function mapping
2007-08-02 14:28:50 Ed_D [Reply | View]
Being that PHP is moving more and more to object-oriented programming, it might be a good idea to recommend that developers use a class map. Define all of the external functions in a class that is then associated with the service.
For example:
class External{
public function getCatalogEntry{...}
}
$server->setClass('External');
$server->handle();
There are several advantages to this approach:
- easier to add/remove functions
- the functions are not in global scope
- when handling SOAP Headers (which are handled as a function call prior to the "actual" function), you can store a value from the header, eg a session id, as an instance variable
- you can use class constants for return/fault codes
- classes make maintaining versions of your service easier (eg use a name like External_1_1). A minor revision upgrade might only mean you need to extend the class and modify a single function (eg External_1_2 extends External_1_1).
Just my 2 cents.
-
PHP Web Services
2007-07-26 22:38:56 MattMan [Reply | View]
Not if the very first step in developing them is to hand code a WSDL file, no. -
PHP Web Services
2007-07-27 06:38:29 Deepak Vohra | [Reply | View]
A WSDL file may be created in a WSDL editor.
http://www.stylusstudio.com/webservices/wsdl_editor.html
http://www.altova.com/products/xmlspy/graphical_wsdl_editor.html



Please tell me how to fixed??
Thanks!